Tag Archives: PowerShell

Active Directory Bulk Manager Update in PowerShell

If you run an organization on Active Directory, it is inevitable that you will have turnover occur that requires a bulk update to the manager fields in Active Directory. You can do this by hand, but that is now what great IT staff does. Great IT staff find a way to script this process knowing they will end up doing it over and over again. Here is a very simple way to do a bulk manager update in Active Directory using PowerShell and Excel.

Build the List of Updated Managers in Excel

First we need to put together an Excel file that holds the user ID and the new manager ID. You can get this list however you would like, but putting it in Excel as seen in the screen shot will get a good start towards making this data available to loop over in PowerShell.

Excel file showing user ID and manager ID columns
Build our list of users to update the manager field in Active Directory.

 

PowerShell Script to Update Active Directory from Excel

Here is our PowerShell script. It is fairly simple. Check the comments to see what the code is doing.

#create excel object
$excelFile=New-Object -ComObject "Excel.Application"
#set file path
$file = "M:\My Documents\Projects\Set Manager\User-Manager.xls"

#open excel file
$excelWorkBook=$excelFile.Workbooks.Open($file)
#set worksheet to the active one
$excelWorkSheet=$excelWorkBook.ActiveSheet

#set our start row
$currRow=2
#get the first userID and managerID values
$userID = $excelWorkSheet.Range("A$currRow").Text
$managerID = $excelWorkSheet.Range("B$currRow").Text

#check if userID and managerID have values(loop will die when either one encountered is blank
IF ($userID -and $managerID) {
	do {
	#make the change
	Get-ADUser -Identity $userID | Set-ADUser -Manager $managerID
	echo "$userID now has manager $managerID"	
	#increment the row
	$currRow++
	#get the next userID and managerID
	$userID = $excelWorkSheet.Range("A$currRow").Text
	$managerID = $excelWorkSheet.Range("B$currRow").Text
	} While ($userID -and $managerID) #loop as long as userID and managerID have values
}
#close workbook
$excelWorkBook.Close()