Can't set multiple ADUser properties at once

Welcome Forums General PowerShell Q&A Can't set multiple ADUser properties at once

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #112807

    Participant
    Points: 0
    Rank: Member

    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: (:) [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)
    
    }
    
    
  • #112811

    Participant
    Points: 0
    Rank: Member

    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)
    

You must be logged in to reply to this topic.