locked
Powershell Attributes change RRS feed

  • Question

  • Hello,

    I'm changing my Active Directory a little bit. And I'd like to put change my department Attribute with the physicaldeliveryofficename. I started a script but its not working... Can anyone help me please? The script:  

    Get-ADUser -filter * | Set-ADUser -add @{physicalDeliveryOfficeName = department}

    Thursday, February 7, 2019 10:36 AM

Answers

  • Try this:

    $User_Department = Get-ADUser -filter * -Properties department | Select-Object SamAccountName,department
    
    
    foreach ($row in $User_Department) {
    
        $row.SamAccountName | Set-ADUser -Add @{PhysicalDeliveryOfficeName = $($row.department)}
    
    }
    

    • Marked as answer by AndreH Thursday, February 7, 2019 12:28 PM
    Thursday, February 7, 2019 11:31 AM
  • The help for Set-ADUser is difficult to understand. The -Add parameter can only be used to add a value if the attribute is multi-valued, and set a value where none exists if the attribute is single valued. The -Replace parameter can be used to update a single valued attribute where an old value exists or not. Unless you are sure the physicalDeliveryOfficeName is currently not set, I would suggest using the -Replace parameter.

    Also, it might help to make sure the department attribute has a value. An error will be raised if you attempt to assign a Null.

    Edit: My suggestion:

    $Users = Get-ADUser -Filter {department -Like "*"} -Properties department
    ForEach ($User In $Users)
    {
        Set-ADUser $($User.distinguishedName) -Replace @{physicalDeliveryOfficeName=$($User.department)}
    }
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Thursday, February 7, 2019 12:08 PM

All replies

  • Try this:

    $User_Department = Get-ADUser -filter * -Properties department | Select-Object SamAccountName,department
    
    
    foreach ($row in $User_Department) {
    
        $row.SamAccountName | Set-ADUser -Add @{PhysicalDeliveryOfficeName = $($row.department)}
    
    }
    

    • Marked as answer by AndreH Thursday, February 7, 2019 12:28 PM
    Thursday, February 7, 2019 11:31 AM
  • It works thank you :) 
    Thursday, February 7, 2019 12:26 PM
  • Thanks it works perfectly 

    Thursday, February 7, 2019 12:28 PM