Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

When to use foreach in a pipeline?

$
0
0

Hello

I keep running into this confusion - when to use foreach cmdlet in a command line and when not to? (Please note I am not talking about foreach loop construct).

For example I have seen the following 2 do not work in the same way:

import-csv input.csv | foreach {
$UserPrincipalName = $_.UserPrincipalName;
Get-ADUser -filter {userprincipalname -eq $userprincipalname} -properties * | foreach { set-aduser $_ -UserPrincipalName "$($_.samaccountname)@test.local"}
}

and

import-csv input.csv | foreach {
$UserPrincipalName = $_.UserPrincipalName;
Get-ADUser -filter {userprincipalname -eq $userprincipalname} -properties * |  set-aduser $_ -UserPrincipalName "$($_.samaccountname)@test.local"
}

The first one above does what I want but not the second one.

Is there any logic to this?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>