bigapple99
bigapple99

Reputation: 305

Get-Member not showing all properties of ADUser

I want to find out all available properties of ADUser. Runnig this command

Get-ADUser -Filter * -Properties * | Get-Member -MemberType properties | Where-Object {$_.name -eq 'ipPhone'}

retruns nothing. Looks like ADuser doesn't have an 'ipPhone' property but it does. Running

Get-ADUser -Filter * -Properties * | select ipPhone 

proves that. Why get-member doesn't reveal all properties of ADuser?

Thanks

Upvotes: 1

Views: 997

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

The behavior you're observing is due to an unfortunate combination of behaviors of the ActiveDirectory cmdlets and Get-Member.

The Get-AD* cmdlets all return "elastic" output objects - they have dynamic properties that only populate if the underlying directory entry has a value in the corresponding Active Directory attribute. As a result, not all properties (like ipPhone, which might be empty/unset for some users, for example) will be populated for each output object.

Get-Member works by inspecting the type of each object that you pipe to it, and only analyze the same type of object once, suppressing any further analysis/output for subsequent objects of the same type.

When you combine these two behaviors, you risk having Get-ADUser output multiple objects that has a specific property (eg. ipPhone) populated, but Get-Member will never pick up on it if the first ADUser object it sees does not have a value for ipPhone.

To work around this, make sure you only pipe objects/users to Get-Member that are certain to have a value in ipPhone:

Get-ADUser -Filter 'ipPhone -like "*"' -Properties * |Select-Object -First 1 |Get-Member -MemberType properties -Name ipPhone

If you need to generate a complete list of properties (say, for Select-Object before exporting the data to CSV, for example), enumerate all the objects and collect the property names individually:

# Fetch all (relevant) objects
$allUsers = Get-ADUser -Filter { Enabled -eq $true } -Properties

# enumerate all possible property names, remove duplicates
$allPropertyNames = $allUsers |ForEach-Object PropertyNames |Sort-Object -Unique

# now we can properly select any possible property
$allUsers |Select $allPropertyNames |Export-Csv path\to\output.csv -NoTypeInformation

Upvotes: 2

Related Questions