Formatting OU in Select-Object expression

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Wei-Yen Tan Wei-Yen Tan 10 months, 1 week ago.

  • Author
    Posts
  • #34814
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Hi guys,

    I am trying to use the get-aduser cmdlet to extract out the display name, email address, and OU.

    I have got the desired results from display name and email address but the OU i am having a bit of trouble formatting. I am using the code below:

    
     Get-aDUser -Filter * -Properties displayname, userprincipalname, distinguishedname -SearchBase "OU=Contractors,OU=BLAH,OU=blah,DC=blah,DC=blah,DC=nz" |Select displayname, userprincipalname, @{N= "Organanisational Unit" ; E = {("($_.DistinguishedName -split ",",2)[1])")}}
    
    

    The formatting tip I got for the OU was found here:

    PS> ($user.DistinguishedName -split “,”, 2)[1]
    CN=Users,DC=Manticore,DC=org
    as looking in the online help I don't think I had the right key words.

    It is still showing the entire line instead of truncating it..

    In any case I think the formatting I chose might have been wrong in this context....I was wondering whether there was any other way that I can format it to format the distinguished name property.

    Thanking you in advance.

  • #34825
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    OUs can be rather long, and Format-Table will truncate things to fit your console window if it needs to. (Format-Table is being used behind the scenes here.)

    If you don't care about the table format, try just piping to Format-List instead.

  • #34829
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    I was curious about this as you quoted my blog post so I tried your code

    what I'm seeing under the Organisational Unit out put is lines like this
    {(CN=Dave Greenly,OU=Testing,DC=Manticore,DC=org.DistinguishedName -split ,...

    In other words its showing you what its trying to do rather than the result.

    The reason is that there's an error in your select statement – specifically the calculated field
    @{N= "Organanisational Unit" ; E = {("($_.DistinguishedName -split ",",2)[1])")}}

    Your expression is effectively outputting 2 strings

    "($_.DistinguishedName -split " which will substitute the value for the distinguished name
    and
    ",2)[1])" which outputs what you see.

    The correct syntax is
    Get-ADUser -Filter * -Properties DisplayName | select DisplayName, UserPrincipalname, @{N= "Organanisational Unit" ;
    E = {($_.DistinguishedName -split ',', 2)[1]}}

    BTW you don't need to put userprincipalname and distinguishedname in the -Properties list as those are default properties that are always shown. -Properties is for properties you want in addition to the default

    You should see the OU in the format you want now. if the output is still too long use format-list as mentioned by Dave or Format-table -wrap

  • #34863
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Hi,

    Thanks guys for the reply.

    @Richard, your revised format gave the right format, and more specifically the difference...

    I used "" in E = {("($_.DistinguishedName -split ",",2)[1])")}}

    instead of ' ' like in yourexample E = {($_.DistinguishedName -split ',', 2)[1]}}

    Thanks once again.

You must be logged in to reply to this topic.