1
votes

Im trying to populate the manager fields on AD using

Import-Csv C:\Testimport.csv | ForEach-Object {Set-ADUser -Identity $_.samAccountName -Replace @{Manager=$_.manager}}

But I get the following error:

Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception setting " Replace": "Object reference not set to an instance of an object." At line:1 char:95 + Import-Csv C:\Testimport.csv | ForEach-Object {Set-ADUser -Identity $.samAcc ountName -Replace <<<< @{manager=$.manager}} + CategoryInfo : WriteError: (:) [Set-ADUser], ParameterBindingEx ception + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
.Management.Commands.SetADUser

2
Have you tried anything to solve this issue so far? - Aleksander Lidtke

2 Answers

1
votes

Depending on what the Manager is represented by in your csv you can just use the Set-Aduser parameter -Manager on its own.

Import-Csv C:\Testimport.csv | ForEach-Object {Set-ADUser -Identity $_.samAccountName -Manager $_.manager}

If not please show some sample date in your question. This would work in Manager was a account name at least. Also there is an error in the code you ran: manager=$.manager should be manager=$_.manager

1
votes

If $_.manager is the Manager sAMAccountName attribute, you need the distinguidhedName attribute to use it later as a value into -Replace parameter:

Import-Csv C:\Testimport.csv | 

ForEach-Object {

    $managerDN = (Get-ADUser $_.manager).distinguishedName

    Set-ADUser -Identity $_.samAccountName -Replace @{Manager=$managerDN}
}