Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, April 1, 2015

Getting User Accounts with identical GivenName and SurName

I am taking a break from writing some custom formatting files to take a look at what is happening on PowerShell.com.  One IT Pro is trying to look for all user accounts with the same GivenName and SurName property values.  He discovered that this will not work.

Get-ADUser -Filter 'GivenName -eq SurName'

 

Here is the error message

Get-ADUser : Error parsing query: 'GivenName -eq SurName' Error Message: 'syntax error' at

position: '15'.

At line:1 char:1

+ Get-ADUser -Filter 'GivenName -eq SurName'

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException

    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADF

   ilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

 

 

The problem is that the Get-ADUser cmdlet is looking for a property on the left and a value to match it to on the right.  It is not looking to match the values of two properties.  At least not in this format.  Here is what I came up with.

 

Get-ADUser -filter * |

    Where-Object -FilterScript {$_.GivenName -eq $_.SurName}

 

 

We are breaking the “Filter to the left” rule because we simply cannot filter the values of to object properties with the Get-ADUser cmdlet.  This is where we can use plan B, Where-Object.  Where-Object is there is the previous cmdlet cannot filter the way you want it to.

 

Well, back to my coding.  Thank you for the break.

No comments: