Formatting OU in Select-Object expression

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:

http://itknowledgeexchange.techtarget.com/powershell/discovering-a-users-ou/
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.

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.

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

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.