How do I export ONE attribute from AD?

This topic contains 3 replies, has 3 voices, and was last updated by  Art Beane 5 years ago.

  • Author
  • #8140


    I need to extract one attribute from certain user accounts in Active Directory, how do I do this?  I tried using the "Get-ADUser -properties ", but it gives me all the basic attributes of the the user, not just the one attribute I want.  So, say for example, I want to get the 'cn' of user JohnHDoe.  I only want it to return 'John' and not all the other stuff.  Thanks.

  • #8144

    Don Jones

    Get-ADUser  | Select -Prop cn

    Get-ADUser always returns a base set of properties. The -Properties parameter specifies additional ones to retrieve. Select-Object specifies any properties of the input object that you want included in the output, if you want something less than the default of "everything."

  • #8332


    So how would I use the 'Select-object' cmdlet to get one attribute from a specific AD user account?  I looked at examples, and I didn't see a way to select a specific user account.  I would want to get the 'cn' from the 'JohnHDoe' AD user account, for example.  Maybe a "Where {$._ }" statement?

  • #8349

    Art Beane

    PowerShell works by default with objects, which have (at least) properties, and are not simple strings like you have in UNIX variants. Even simple strings in PowerShell are objects. But, you can format an object to output it for human consumption in a way that looks like a simple string. As Don said, Select-Object is one of those tools. So, I think that what you want to do is best performed by using the -ExpandProperty switch with Select-Object. This will output just a plain "John" for you:

    Get-ADUser "JohnHDoe" | Select-ExpandProperty CN

You must be logged in to reply to this topic.