Kyra
Kyra

Reputation: 27

Powershell - parse Get-ADUser property

I want to scan Active Directory, and output to a .csv, the 'User Account' and 'Manager ID'. Get-ADUser outputs the Manager property as a DN. What I want in the second column is the employee number of the manager. The end result should look like this:

User Account        Manager  
John                4342  
Jack                5432   

Here is the script:

$csvmaster = @()
$uacct = 'User Account'
$manager = 'Manager'

$ulist = get-aduser -filter {enabled -eq 'true'} -properties * | ? {$_.title -ne $null} | select GivenName, Surname, EmailAddress, EmployeeNumber, Title, Manager, Department, samaccountname -first 5

foreach ($u in $ulist)
{
$csvline = New-Object System.Object
$csvline | Add-Member -MemberType NoteProperty -name $uacct -value $u.givenname
$csvline | Add-Member -MemberType NoteProperty -name $manager -value (Get-aduser $u.manager -Properties * | select employeenumber | ? { $_ -match '\d'})
$csvmaster += $csvline
}    
$csvmaster #Test output to console  

Here is what I am getting:

User    Manager  
John    @{employeenumber=1238}  
Jack    @{employeenumber=1593}  

The output value is correct. That is to say, I am getting the Manager's ID, I just want to display the number only. I tried regex matching, but I'm not sure if the fact the output is a PSCustomObject keeps it from working, because the output doesn't change if I remove the | ? { $_ -match '\d'} from the foreach loop. I'm not sure if I'm going about this the right way, either.
Cheers

Upvotes: 0

Views: 2225

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36277

This one's easy... You need to use the Select cmdlet's -ExpandProperty switch, as such:

$csvline | Add-Member -MemberType NoteProperty -name $manager -value (Get-aduser $u.manager -Properties * | select -Expand employeenumber | ? { $_ -match '\d'})

Then instead of getting an ADUser object with 1 property you get a string object that is the value of employeenumber.

Upvotes: 1

Related Questions