Powershell (Get-ADUser -identity).someproperty not showing result

active-directorypowershell

I am trying to search for individual properties of an AD-User, when I run

(Get-ADUser $userName).$targetProperty

I get nothing
When I run

Get-ADUser $userName | select $targetProperty

I get the property and a blank underneath.
BUT when I run.

Get-ADUser $user -Properties * 

I get the full list & when I look at $targetProperty, there is a value already there.
My environment is 1 DC, its local, and I am in Admin mode.

Best Answer

It depends on the target property you are trying to get. If it is not one of the properties in the default set (the ones you get when you just run Get-ADUser testuser), it isn't in the result set, so you can't get to it elsewhere in your command.

Here are the default properties.

DistinguishedName : CN=Test User,CN=Users,DC=domain,DC=local
Enabled           : True
GivenName         : Test
Name              : Test User
ObjectClass       : user
ObjectGUID        : 94916bd1-008e-409f-a1b3-eaXXXXXX266e
SamAccountName    : TestUser
SID               : S-1-5-21-23XXXXXX88-41XXXXX83-1941XXXX34-32584
Surname           : User
UserPrincipalName : TestUser@example.com

Let's say the property you want is Title. So your first command is this:

(Get-ADUser testuser).Title

Title is not in the default set of properties, so the part in parentheses doesn't include a Title property, so the result of the combined command is nothing.

Second attempt:

Get-ADUser testuser | select Title

That gives you the word "Title" as the header of the column, but there is still no value for it, because the first part of the command still doesn't include a Title property in the results.

Third attempt:

Get-ADUser testuser -Properties * 

Aha! That's getting close. It includes the Title property, along with a whole bunch of others that you don't care about right now.

Fourth attempt:

(Get-ADUser testuser -Properties *).title

That's it! This combination gets all of the properties of the user (so that includes Title) and then only shows you the Title property.

Optional: You can make it a bit faster by not bothering to ask for the 100+ properties that -Properties * gives you, and only get the default properties plus Title, with this:

(Get-ADUser testuser -Properties Title).Title