Take more caracters in Get-ADuser

Hi Folks I am new on powershell commands and serch on internet I did a simple script that I need, however, one of the collumns is trucated and I need at least more 10 caracters. Look below, please:

Get-ADUser -filter * - properties * | select givenname, manager | c:\test.txt

the manager column, in the file out like below:
CN=Shatamnu.Wellin…

How can I extend this line? I need all name that is Shatamnu.Wellington for example

Thank you

WILSON ROSA,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

I’d recommend using a CSV file instead of plain text. This way you get the complete property instead of the truncated string.

Regardless of that - it is a bad idea to request ALL properties when you just need a single one more than the default set of properties the cmdlet Get-ADUser returns. Instead you should specify only the properties you’re after. This reduces the stress you put on your AD and it speeds up your code.
Besides this it is recommended to use a searchbase (-SearchBase) to limit your query to a particular OU and the sub OUs.

So something like this would be good start:

$SearchBase = 'OU=Users,OU=Germany,OU=Europe,DC=contosoo,DC=com'
Get-ADUser -Filter * -SearchBase $SearchBase -Properties Manager | 
    Select-Object -Property GivenName, Manager | 
        Export-Csv -Path '.\ADUserList.CSV' -NoTypeInformation
1 Like

Give this a bash:

get-aduser -filter * -properties  givenname, manager | select givenname,@{n='Managername';e={($_.manager.split(',')[0]).replace('CN=','')}} | export-csv c:\test.csv -NoTypeInformation

What if the CN of the manager actually has a comma in it? :thinking: Some companies use something like Lastname, Firstname (Department) as their CN.

And BTW: givenname is part of the default subset of properties Get-ADUser returns by default. You don’t need to request it with the parameter -Properties explicitly. :wink:

I know - we use commas. I just tossed something off given the example seemed to be firstname.lastname. Personally we use regex to parse a DN to extract the RDN.

Well … in this case I’d recommend to use something like this:

$SearchBase = 
   'OU=Users,OU=Germany,OU=Europe,DC=contosoo,DC=com'
$AllADUsersList = 
    Get-ADUser -Filter * -Properties Manager, DisplayName -SearchBase $SearchBase
$ManagerLookupTable = 
    $AllADUsersList | 
        Select-Object -Property DistinguishedName, DisplayName | 
            Group-Object -Property DistinguishedName -AsHashTable


$AllADUsersList |
    Select-Object -Property GivenName,
        @{Name = 'ManagerDisplayName'; Expression = {$ManagerLookupTable[$_.Manager].DisplayName}} |
            Export-Csv -Path '.\AllUsersListWithManager.csv' -NoTypeInformation

:wink:

This way you don’t have to tinker arround string acrobatics with regex. :man_shrugging:t3:

Hi Folks
Than kyou for the quick reply, but, as I am new in PS, I think I need a simple query like below, I need to know username, accountexpirationdate, mail and managername only this. I did these to line codes but the file after run the code is empty, text or csv.

Get-ADUser -filter {accountexpirationdate -ne "$null"} -properties * | select givenname, accountexpirationdate, mail, manager | out-file c:\temp\terceiros.txt

Get-ADUser -filter {accountexpirationdate -ne "$null"} -Properties * | select givenname, accountexpirationdate, mail, manager | export-Csv -Path '.\test.csv' -NoTypeInformation

Am I correct? if yes, why the file always is empty?

Thank you

You’ve stated something else in your initial question. :man_shrugging:t3:

Have you tried the code snipped we suggested?

If you want to check where in your pipeline an error might be you run the single elements of separately …

Does …

Get-ADUser -filter { $null -ne accountexpirationdate } -properties accountexpirationdate, mail, manager

… output anything?

Here you can read why I changed the order of the elements in your comparison:

1 Like

Hi Folks
After some tips I think I got it.
Thank you

get-aduser -filter {accountexpirationdate -ne "$Null"} -Properties accountexpirationdate, mail, manager | Select-Object accountexpirationdate, mail, manager | export-csv c:\temp\test.csv -NoTypeInformation

yeah i had originally noted that the post with the complexity was probably a bit too much but decided not to post, figured i was being a grump lol.

I think calculated properties + chaining methods + splitting and then pulling data from the resulting array on an object is perhaps a tad much. It also made the assumption of what they wanted (OP noted 'extending, not extracting the CN).

1 Like

Thank you Folks
You helped a lot
Until Next one :slight_smile:

Happy you got something working. I’m going to mark @Olaf’s answer as the ‘accepted’

1 Like