Can't set multiple ADUser properties at once

Hello

I can set multiple records at once as listed below.

$users = Import-Csv -path C:\Temp\User.csv

 

foreach ($user in $users) {

 

Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" -Properties * | Set-ADUser -ScriptPath $($User.scriptpath), -homeDrive $($User.homedrive), -homedirectory $($User.homedirectory)

}

Getting error.

Set-ADUser : Cannot convert ‘System.Object[]’ to the type ‘System.String’ required by parameter ‘ScriptPath’. Specified method is not supported.

At line:5 char:110

  • … es * | Set-ADUser -ScriptPath $($User.scriptpath), -homeDrive $($User …

  •                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  • CategoryInfo : InvalidArgument: (:slight_smile: [Set-ADUser], ParameterBindingException

  • FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser

 

But if I set these one at a time it works.

$users = Import-Csv -path C:\Temp\User.csv

 

foreach ($user in $users) {

 

Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" -Properties * | Set-ADUser -ScriptPath $($User.scriptpath)

}

foreach ($user in $users)

{

 

Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" -Properties * | Set-ADUser -homeDrive $($User.homedrive)

}

foreach ($user in $users)

{

 

Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" -Properties * | Set-ADUser -homedirectory $($User.homedirectory)

}

You need to remove the commas:

Set-ADUser -ScriptPath $($User.scriptpath), -homeDrive $($User.homedrive), -homedirectory $($User.homedirectory)

It should be:

Set-ADUser -ScriptPath $($User.scriptpath) -homeDrive $($User.homedrive) -homedirectory $($User.homedirectory)