Assignment Operators Can Get in Your Way

by May 6, 2010

Have a look at this line – do you see what’s wrong with it?

Get-WMIObject Win32_UserAccount |
Where-Object { $_.Name = ‘Guest’ }

PowerShell will use different operators for assignment and equality. So you may run into these issues if you have scripted in another language before. The preceding illegally used an assignment operator instead of a comparison operator. So you can see that Where-Object actually tried to assign a new name to the user object, instead of checking the name. This is how it should look:

Get-WMIObject Win32_UserAccount |
Where-Object { $_.Name -eq ‘Gast’ }

Twitter This Tip! ReTweet this Tip!