Unintuitive expression behavior in pipelines

It looks like you cannot use expressions with $_ when submitting parameters to a cmdlet in a pipeline.

This means that the examples below do not work:

# Give all users passwords based on their samAccountNames
Get-QADUser | Set-QADUser -UserPassword $_.samAccountName

# Append a postfix to each name
Get-QADUser -City London | Rename-QADObject -NewName ($_.Name + " - London")

# Add a prefix to each file in the current folder
dir |Rename-Item -NewName ("_" + $_.Name)

No matter how intuitive these look to you, in reality they don’t work. All these $_.something get evaluated to empty strings and thus users get blank passwords, and names get changed to just the prefixes/postfixes while losing the main parts.

This happens because PowerShell v1 evaluates the expressions not for each item in the pipeline but once before items get passed to the cmdlet. So the second cmdlet in the pipeline just does not get the parameters.

The workaround is to use ForEach-Object instead of direct pipeline. So the examples above would be changed to:

# Give all users passwords based on their samAccountNames
Get-QADUser | ForEach-Object { Set-QADUser $_.DN -UserPassword $_.samAccountName }

# Append a postfix to each name
Get-QADUser -City London | ForEach-Object { Rename-QADObject $_.DN -NewName ($_.Name + " - London") }

# Add a prefix to each file in the current folder
dir |ForEach-Object { Rename-Item $_ -NewName ("_" + $_.Name) }

Specifically for the last example you could also use scriptblock instead of the expression and this will work because PowerShell team implemented scriptblock support for this particular cmdlet:

# Add a prefix to each file in the current folder
dir |Rename-Item -NewName {"_" + $_.Name}

I don’t know why was this specific design decision made, and I wish the syntax cited in the beginning of the post (and which I consider much more intuitive!) worked. If anyone from PowerShell team is reading – please consider this a feature request in addition to the security and other v2 requests I made before.😉

For additional discussions see these forum/newsgroup threads:

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

September 2007
M T W T F S S
« Aug   Oct »
 12
3456789
10111213141516
17181920212223
24252627282930

%d bloggers like this: