Help with Get-ADUser Output!?!?!

In short I have made a little GUI tool to create Super accounts in our domains. This tool will get the attributes from the user's regular account by making them into a variable and using that variable during the new-aduser cmd to set the attribute. I am having trouble with a command, the command gets what I need but not in the right format. Here is what I am doing.

When the button is clicked to initiate the cmd it will first go through setting variables and one of the examples I am having trouble with is:

$StAddress = Get-ADUser $textfield1.text -Properties * | Select-Object Streetaddress

This returns:

StreetAddress

---------------

255 Park Ave

 

I can even use:

$StAddress = Get-ADUser $textfield1.text -Properties * | Select-Object Streetaddress  | FT -HideTableHeaders

And this produces:

255 Park Ave

 

When I am setting the Street address it will set but it will be this

@{Streetaddress=2555 Park Ave}

instead of 255 Park Ave

 

When I am doing any of the properties from the Get-member cmd I can just use dotted notation and I don't have this problem i.e. (get-aduser textfield.text).Givenname but that wont work with any properties outside the Get-aduser <User> | GM cmd

 

Any Ideas??

 

  • You were on the right track by using the Select-Object cmdlet, the parameter you are looking for is -ExpandProperty, this only outputs the value in the property rather than the key-value pair. Here is what this looks like in code:

    $StAddress = Get-ADUser $textfield1.text -Properties * | Select-Object -ExpandProperty Streetaddress

    Alternatively you could encapsulate the entire command in Parentheses and select the property you require:

    $StAddress = (Get-ADUser $textfield1.text -Properties *).Streetaddress

    Does that answer your question?

  • Another note, you want to avoid using -Properties * when you can as you're recalling EVERY field from AD that is associated with the user.  If you're getting all of your users this could really affect performance.  Try to limit the Properties parameter to just what you need:

    Get-ADuser $textfield1.txt -Properties StreetAddress | Select -ExpandPropert StreetAddress

  • That worked!

    Wow I didn't even think about doing that, I feel dumb!! I was using that for the Get-member parameters. (Get-aduser <user>).samaccountname 

    I never tried it by throwing -properties * in the equation.

    I really appreciate the help.

     

  • "Filter left, format right."

    Proof once again that there are a lot of ways you can get the job done in PowerShell.  However,  not all solutions are equally efficient.  In small jobs you probably won't notice the difference.  However, as jobs grow to enterprise-scale, performance can become a major factor (both on the systems performing the operations and the humans waiting for the results).  The earlier you can filter in the process, the more efficient the process.

    There's almost a little competition here to see who can come up with the most efficient algorithm.  To that end, Measure-Command will reveal the good, the bad, and the ugly.  It is your friend.

    Come back often.  There seems to be no end to the learning in PowerShell land.  And feel free to contribute as you gain your own insights and experience.  I'm on vacation, but I still try to keep up here because PowerShell is just outright fun.

  • It works for almost any cmdlet, depending on the version of Powershell you are using and the output of the cmdlet (array or single object). No reason to feel dumb, feel free to stick around and ask questions as it is a good way to learn. We can even point you in the right direction if you are looking to go a bit more in depth with it.