Some records have account type N and others have account type G in this example.
Why does the filter work in this script:
Get-Content "MembersMS.txt" | ForEach-Object {Get-ADUser -Identity $_ -Properties name, uht-IdentityManagement-AccountType} | Where-Object {($_['uht-IdentityManagement-AccountType'] -ne "N")}
i.e., you get only the G records, but not this one:
Get-Content "MembersMS.txt" | ForEach-Object {Get-ADUser -Identity $_ -Properties * | select name, uht-IdentityManagement-AccountType} | Where-Object {($_['uht-IdentityManagement-AccountType'] -ne "N")}
i.e., you get both the N and G records.
The extra "pipe" in the second script allows for a much nicer format since you get much less data per record; you get columnar output, one row per record.
I have tried -notlike "N" to no avail.
The issue is not with the properties or filtering but with the position of the
Selectstatement, and how it changes the following statements in the pipeline.The first statement:
Can be broken up into 2 statements:
Similarly, the second statement:
Can be broken up into 2 statements:
Both statements generate arrays:
But the objects inside the array are different.
The first is an array of Type
ADAccountand the second is an array ofSytem.Objectaccounts. This is ok if you are iterating over (and filtering over) the items inside the array. But this is different when you are iterating/filtering over properties inside the array.The way you use
Where-Objecttries to invoke theGetEnumerator()Method, which the TypeADAccountconveniently has, butSystem.Objectdoes not.Therefore it can't enumerate over the properties and properly perform the
Where-Objectfiltering.The better method is to do the "formatting" of
Selectat the end of the pipeline: