powershellactive-directoryattributes

Windows PowerShell error when attempting to bulk edit the Cost Center (costCenter) attribute


I'm trying to change the "costCenter" attribute. I noticed in the Schema the description was originally "Cost Center". I thought maybe the space was an issue so changed it to "Cost-Center". The attribute name is "costCenter". I have no idea what I'm missing here. Any help appreciated.

Below is the script:

Import-Module ActiveDirectory

$Attribcsv=Import-csv "C:\Temp\ADUsers_BulkEdit.csv" ForEach ($User in $Attribcsv)

{ Get-ADUser -Identity $User.samAccountName | set-ADUser -"Cost-Center" $($User.costCenter) }

The error that keeps spitting out is below:

Set-ADUser : A positional parameter cannot be found that accepts argument 'MM'. At line:5 char:45



I've run the script with different attributes--for testing purposes--successfully:
1. Using the "description" attribute under the General tab in AD
2. Using the "comment" attribute under the Attribute Editor tab in AD

Both of those worked successfully

Solution

  • To add / replace / remove custom attributes or those attributes that do not exist as a Parameter for that cmdlet you have to use -Add / -Replace / -Remove.

    So assuming you want to change the attribute value, and that attribute name is Cost-Center then you can use -Replace:

    $Attribcsv = Import-Csv 'C:\Temp\ADUsers_BulkEdit.csv'
    foreach ($User in $Attribcsv) {
        Get-ADUser -Identity $User.samAccountName |
            Set-ADUser -Replace @{ 'Cost-Center' = $User.costCenter }
    }