1

I have a one line powershell command to extract managers from active directory, but it only gives me one attribute. What can I do to get more attributes like name, sAMAccountName and employeeNumber.

The command below just gives the employeeNumber of manager.

Get-ADUser -Filter * -SearchBase "OU=Users,OU=Office365,DC=example,DC=lan" -Properties * | Select-Object name, employeeNumber, @{Name='Manager';Expression={(Get-ADUser -Property employeeNumber $_.Manager).employeeNumber}} | export-csv -path c:\temp\userexport.csv

Desired csv file headers:

name    employeeNumber   ManagerName      ManagerEmployeeNumber
3
  • You can retrieve multiple values and join them together with a delimiter. However, that particular cell in your CSV will now have a list of items. Commented Mar 19, 2021 at 18:19
  • Can you show an example of how you want the resulting CSV file to look? Commented Mar 19, 2021 at 18:20
  • @MathiasR.Jessen Just updated the post with desired csv columns. Commented Mar 21, 2021 at 18:31

1 Answer 1

2

If you are attempting to store multiple values in a single cell of a CSV file, you will have to create a delimited list within that cell.

# Calculated property joining property values with ;
# Use try-catch because some users may not have a manager
# No manager will throw a [ParameterBindingValidationException] exception.
$ManagerProp = @{
    Name='Manager'
    Expression={
        try {
            $user = Get-ADUser $_.Manager -Properties EmployeeNumber
            "{0};{1};{2}" -f $user.Name,$user.SamAccountName,$user.EmployeeNumber
        }
        catch { }
    }
}
Get-ADUser -Filter * -SearchBase "OU=Users,OU=Office365,DC=example,DC=lan" -Properties EmployeeNumber,Manager |
    Select-Object Name,EmployeeNumber,$ManagerProp |
        Export-Csv -Path c:\temp\userexport.csv -NoType

-f is the String format operator.

Note that it is not efficient to use -Properties * because of the overhead required to retrieve unneeded properties.


You could give each Manager property its own property:

Get-ADUser -Filter * -SearchBase "OU=Users,OU=Office365,DC=example,DC=lan" -Properties EmployeeNumber,Manager |
    Foreach-Object {
        $obj = [pscustomobject]@{
            Name = $_.Name
            EmployeeNumber = $_.EmployeeNumber
            Manager_Name = ''
            Manager_SamAccountName = ''
            Manager_EmployeeNumber = ''
        }
        if ($_.Manager) {
            $Manager = Get-ADUser $_.Manager -Properties EmployeeNumber
            $obj.Manager_Name = $Manager.Name
            $obj.Manager_SamAccountName = $Manager.SamAccountName
            $obj.Manager_EmployeeNumber = $Manager.EmployeeNumber
        }
        $obj
        }
    } | Export-Csv -Path c:\temp\userexport.csv -NoType
                           
Sign up to request clarification or add additional context in comments.

2 Comments

Nice examples and explanation, this should be tagged as resolved.
Hi, thanks for the explanation. I don't want to store the attributes in the same cell. I basically want 4 columns in my csv file - Name, EmployeeNumber, Manager Name, Manager EmployeeNumber. I am sorry, I am not a powershell expert, just trying to make best use of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.