Home > Software design >  Update user AD attributes from CSV file using powershell
Update user AD attributes from CSV file using powershell

Time:01-18

I'm trying to update AD user attributes:title, physicalDeliveryOfficeName and department from CSV file using powershell. I am new to powershell so I need some help. (please, and thanks in advance) So, the idea is that filter for a match is displayName attribute and the script I use is:

Import-Module ActiveDirectory 
$csv = Import-Csv C:\Temp\AD_import_test.csv 
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($csv.title) -department $($csv.Department) -physicalDeliveryOfficeName $($csv.physicalDeliveryOfficeName) }

But the script is returning error:

 Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
    At line:6 char:19
      Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
                        ~~~~~~~~~~~~~
          CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
          FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
  Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
                    ~~~~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
      FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser 

CodePudding user response:

You code seems good for me except the set-aduser you need to replace $csv with $line

Import-Module ActiveDirectory 
$csv = Import-Csv C:\Temp\AD_import_test.csv 
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }

CodePudding user response:

You're using the wrong variable to assign those values. In your script $csv contains the entire .csv file, but within your foreach line you're loading each individual line into $line, and it's that which you need to set your value.

So in essence, $($csv.title) equals ALL of the title fields, not just the one you're interested in. That's why you're getting the error saying it's a System.Object[] rather than System.String... because it's not one single value.

I notice you have it correct on the "$displayName = $line.displayName" line, so it's just the Set-ADUser line that needs updating, as :

Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }

Edit: Also, with something like this a useful trick is to add some Write-Output lines to show what the variable contents are, so you can check if they match what you're expecting, for instance :

Write-Output $($line.title)

which then in the case of $line.title should show you a single value, whereas if you did $($csv.title) it would return multiple values and show you where the issue is.

  •  Tags:  
  • Related