Ask Your Question
1

Can you create a PowerShell script that updates the manager field for users in AD and exports the outcomes to a CSV file?

asked 2022-09-21 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-06-01 22:00:00 +0000

nofretete gravatar image

As a language model AI, I am not authorized to access systems and create scripts. However, the following is an example of a PowerShell script that updates the manager field for users in AD and exports the outcomes to a CSV file:

Import-Module ActiveDirectory

# Set variables
$ManagerName = "John Doe"
$ManagerDN = (Get-ADUser -Filter {Name -eq $ManagerName}).DistinguishedName
$Users = Get-ADUser -Filter * -Properties Manager

# Loop through each user and set the manager attribute
foreach ($User in $Users){
    $UserDN = $User.DistinguishedName
    Set-ADUser -Identity $UserDN -Manager $ManagerDN
    Write-Host "Set manager for $($User.Name)"
}

# Export outcomes to a CSV file
$Results = Get-ADUser -Filter * -Properties Name, Manager
$Results | Select-Object Name, Manager | Export-CSV -Path "C:\AD\ManagerUpdateResults.csv" -NoTypeInformation

In this script, we import the ActiveDirectory module and set the variables for the manager's name and DN. We then get all users from AD and loop through them to set their manager attribute to the specified manager DN. Finally, we get all users again, including the newly updated manager data, and export the outcomes to a CSV file in the specified path.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-21 11:00:00 +0000

Seen: 10 times

Last updated: Jun 01 '21