Update managers for bulk AD users

by athreyatoo43 at 2013-01-29 23:21:11

Could someone please help me in updating the manager attribute from an excel file for multiple users using powershell.
i am very new to powershell, i just know that this task can be done by powershell but no idea how to do it.
my manager gave me a task of updating managers for 2500 users. please help
by Klaas at 2013-01-30 00:30:48
Can you show us the code you have so far, and explain what goes wrong or where you’re stuck?
by athreyatoo43 at 2013-01-30 03:36:22
I got the below code

$list = Import-Csv .\User-Manager.csv

$list | Foreach { $boss = Get-ADUser -Identity $.Manager ; Set-ADUser -Identity $.User -Replace @{manager=$boss.DistinguishedName} }

I have created a test CSV file called User-Manager whisch is attached.

>And when i run the above script i get the below error


The term ‘Get-ADUser’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:37
+ $list | Foreach { $boss = Get-ADUser <<<< -Identity $.Manager ; Set-ADUser -Identity $.User -Replace @{manager=$bo
ss.DistinguishedName} }
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
by athreyatoo43 at 2013-01-30 03:37:59
This is my Test CSV file , attached
by Optimal at 2013-01-30 10:14:32
Import-Module ActiveDirectory
by athreyatoo43 at 2013-01-31 03:27:30
I get the below error when i run
Import-Module ActiveDirectory

*
PS P:&gt; cd c:<br>PS C:&gt; Import-Module ActiveDirectory
Import-Module : The specified module ‘ActiveDirectory’ was not loaded because no valid module file was found in any mod
ule directory.
At line:1 char:14
+ Import-Module <<<< ActiveDirectory
+ CategoryInfo : ResourceUnavailable: (ActiveDirectory:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
by athreyatoo43 at 2013-01-31 04:10:06
Can some please help me !!!
by kittH at 2013-01-31 05:15:27
http://blogs.msdn.com/b/rkramesh/archiv … ows-7.aspx

You need to install the Remote Server Administration Tools to get the activedirectory powershell module.
by athreyatoo43 at 2013-01-31 06:45:46
Windows Server 2003 SP1 Administartion Tools Pack is installed on my server.
So is this different from Remote server Administration tools ?
by athreyatoo43 at 2013-01-31 07:09:54
Now i get
/

Set-ADUser : Cannot bind parameter ‘Replace’ to the target. Exception setting "Replace": "Object reference not set to a
n instance of an object."
At line:1 char:98
+ $list | Foreach { $boss = Get-ADUser -Identity $.Manager ; Set-ADUser -Identity $.User -Replace <<<< @{manager=$bo
ss.DistinguishedName} }
+ CategoryInfo : WriteError: (:slight_smile: [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADUser
by kittH at 2013-01-31 12:11:56
Ok, doing this as a one liner is possible, but lets break it out a little bit so it’s easier to understand:

#Import the Active Directory Module
Import-Module ActiveDirectory

#Save the CSV file contents to the $CSV Variable
$CSV = Import-Csv C:\User-Manager.csv

#Foreach row in the CSV, do the following:
Foreach ($Row in $CSV){

#Save the Username as it's own variable
$UserName = $Row.User

#Save the Manager name as it's own variable
$ManagerName = $Row.Manager

#Get the user object that matches the username
$User = Get-ADUser -Filter {Name -eq $Username}

#Get the user object that matches the manager name
$Manager = Get-ADUser -Filter {Name -eq $ManagerName}

#Set the manager field on the user object
Set-ADUser -Identity $User -Manager $Manager}
by Klaas at 2013-02-01 00:39:49
First, check if the import and get-aduser part works:
$list | Foreach { $boss = Get-ADUser -Identity $_.Manager ; $boss}
Do you see the output you expect?
if so, maybe the -replace can’t handle dot-notation. You can try to ecapsulate in $(): $($boss.distinguishedname)
by athreyatoo43 at 2013-02-01 11:43:53
PS P:&gt; cd c:<br>PS C:&gt; cd users
PS C:\users> cd ushasp
PS C:\users\ushasp> cd desktop
PS C:\users\ushasp\desktop> Import-Module ActiveDirectory
PS C:\users\ushasp\desktop> $CSV = Import-Csv User-Manager.csv
PS C:\users\ushasp\desktop> Foreach ($Row in $CSV){ $UserName = $Row.User $ManagerName = $Row.Manager $User = Get-ADUser
-Filter {Name -eq $Username} $Manager = Get-ADUser -Filter {Name -eq $ManagerName} Set-ADUser -Identity $User -Manager
$Manager }
Unexpected token ‘ManagerName’ in expression or statement.
At line:1 char:59
+ Foreach ($Row in $CSV){ $UserName = $Row.User $ManagerName <<<< = $Row.Manager $User = Get-ADUser -Filter {Name -eq
$Username} $Manager = Get-ADUser -Filter {Name -eq $ManagerName} Set-ADUser -Identity $User -Manager $Manager }
+ CategoryInfo : ParserError: (ManagerName:String) , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
by athreyatoo43 at 2013-02-01 11:49:14
i get an error for the "ManagerName", not sure y ?
by kittH at 2013-02-01 12:18:31
Because you can’t just paste it all as one line, you can paste it a line at a time, or save it as a .ps1 file and run it as a script.
by ArtB0514 at 2013-02-01 12:28:22
Alternatively, if you really want to keep everthing in a single line, then separate each command element with ";":

Foreach ($Row in $CSV){ $UserName = $Row.User;$ManagerName = $Row.Manager;$User = Get-ADUser -Filter {Name -eq $Username};$Manager = Get-ADUser -Filter {Name -eq $ManagerName};Set-ADUser -Identity $User -Manager $Manager.DistinguishedName }

Also, make sure that you use the appropriate property from the $Manager object when setting the user property. Use this to see what is expected:
Get-Help Set-ADUser -Property Manager
by athreyatoo43 at 2013-02-01 13:36:02
PS C:\users\ushasp\desktop> Foreach ($Row in $CSV){ $UserName = $Row.User;$ManagerName = $Row.Manager;$User = Get-ADUser
-Filter {Name -eq $Username};$Manager = Get-ADUser -Filter {Name -eq $ManagerName} Set-ADUser -Identity $User -Manager
$Manager }
Get-ADUser : Cannot validate argument on parameter ‘Identity’. The argument is null. Supply a non-null argument and try
the command again.
At line:1 char:197
+ Foreach ($Row in $CSV){ $UserName = $Row.User;$ManagerName = $Row.Manager;$User = Get-ADUser -Filter {Name -eq $Usern
ame};$Manager = Get-ADUser -Filter {Name -eq $ManagerName} Set-ADUser -Identity <<<< $User -Manager $Manager }
+ CategoryInfo : InvalidData: (:slight_smile: [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
by kittH at 2013-02-01 13:44:22
You’re missing the first command, where you Import the CSV file.

Also, looking over your CSV file it looks like you have a mix of name types so I’m not sure it’ll resolve them all correctly. (IE: Eric Maze is a fullname, cprotzman sounes like a username)
by athreyatoo43 at 2013-02-01 13:50:48
I have ran the import CSV and import AD commands also (Which i just dint paste there).
now i am not using the csv file which u mentioned. i am using a new one which has all the names format as Nick Reed , Valorie Rogers etc
by athreyatoo43 at 2013-02-01 13:52:22
i just ran a small command from the -help examples

PS C:\users\ushasp\desktop> Import-Module ActiveDirectory
PS C:\users\ushasp\desktop> Get-ADUser -Identity "Nick Reed" | Set-ADUser -Manager "Valorie Rogers"
Get-ADUser : Cannot find an object with identity: ‘Nick Reed’ under: ‘DC=jsi,DC=jc,DC=com’.
At line:1 char:11
+ Get-ADUser <<<< -Identity "Nick Reed" | Set-ADUser -Manager "Valorie Rogers"
+ CategoryInfo : ObjectNotFound: (Nick Reed:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: ‘Nick Reed’ under: ‘DC=jsi,DC=jc,DC=com’.,Microsoft
.ActiveDirectory.Management.Commands.GetADUser
by athreyatoo43 at 2013-02-01 13:54:02
Nick Reed and Valorie Rogers are existing in my domain. Not sure y this error…
If we take that there is something wrong in the file or importing it, then it should atleast update this single user manager details with this direct command
by kittH at 2013-02-01 14:10:25
You have to use the -Filter command to search on full name, like in the script I posted.

Get-ADUser -Filter {Name -eq $Username}

For your example it would be Get-ADUser -Filter {Name -eq "Valerie Rogers"}

It may be easier for you to work with the Quest Active Directory module since it is much more forgiving on identity input.
http://www.quest.com/powershell/activeroles-server.aspx
by athreyatoo43 at 2013-02-02 00:12:59
Finally this worked…

PS C:\users\ushasp\desktop> Import-Module ActiveDirectory
PS C:\users\ushasp\desktop> $CSV = Import-Csv User-Manager.csv
PS C:\users\ushasp\desktop> Foreach ($Row in $CSV){$UserName = $Row.SamAccountName;$ManagerName = $Row.Manager;$User = G
et-ADUser -Filter {SamAccountName -eq $Username};$Manager = Get-ADUser -Filter {SamAccountName -eq $ManagerName};Set-ADU
ser -Identity $User -Manager $Manager}


I went by filtering the SamAccountName. I got the result what i wnted

Can i get a command for getting the samaccountname of multiple users for the given input of a display names ?
by athreyatoo43 at 2013-02-02 05:24:56
Thanks a lot for this forum and all who helped me… This is awesome…

My special thanks to kittH… Love you sir. Hatsoff.

Finally Can i get a command for getting the samaccountname of multiple users for the given input of a display names , output to an excel file?
by athreyatoo43 at 2013-02-02 05:36:12
I have this

$UserName = Get-ADUser -LDAPFilter "(displayName=Jim L. Smith)" | Select sAMAccountName

How can i make this to read the input from an excel and output to an excel.
by athreyatoo43 at 2013-02-02 06:18:03
wen i try the above the script , i neither get any output nor any error…
no idea is it having the output in buffer ?