I have a PowerShell script that gavers AD attibutes Sammaccount,givenname, manager, office

Nick Anderson 0 Reputation points
2023-03-20T15:06:29.1133333+00:00

I have a powershell script which retrieves the Attribute information from Active Directory and then creates the CSV file.

The Attributes all come through however the manager attribute brings with it the OU paths

this is script and the CSV it create all is working but dont want the OU path that come with the manager atribute

$OU = "OU=User Accounts,OU=Accounts,OU=Common,OU=GB,OU=Users,OU=test,OU=OMEGA,DC=corp,DC=domainname-group,DC=com"

$CSVname = "C:\Users\user\Documents\outputresult.csv"

Get-ADUser -SearchBase $OU -Filter * -Properties SamAccountName,GivenName,Surname,EmailAddress,Department,Manager,Office| Select SamAccountName,GivenName,Surname,EmailAddress,Department,Manager,Office| Export-Csv $csvname

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-03-20T15:25:31.64+00:00

    Try this:

    $OU = "OU=User Accounts,OU=Accounts,OU=Common,OU=GB,OU=Users,OU=test,OU=OMEGA,DC=corp,DC=domainname-group,DC=com"
    
    $CSVname = "C:\Users\user\Documents\outputresult.csv"
    
    Get-ADUser -SearchBase $OU -Filter * -Properties SamAccountName,GivenName,Surname,EmailAddress,Department,Manager,Office |
        Select-Object SamAccountName,GivenName,Surname,EmailAddress,Department,@{n='Manager';e={(Get-ADUser $_.Manager).saMAccount}},Office | 
            Export-Csv $csvname
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.