Forum Discussion

Denise Child's avatar
Denise Child
Iron Contributor
Jul 21, 2022

bulk replace or clear two attributes in Active Directory - (Info and ipPhone)

Hello,

 

We need to replace or clear two attributes in Active Directory for many people.

Those attributes are "info" and "ipPhone".

 

I can use -Clear on an individual user, but am unable to find how to do this in bulk with a CSV.

 

Set-ADUser -Identity username -Clear info

 

I can also use -Replace to add information

Set-ADUser -Identity username -Replace @{"info"=("Test1,Test")}

 

Can someone help me with this?

My CSV would use properties:

sAMAccountName, info, ipPhone

 

Thanks for any help.

 

Denise

 

 

 

 

  • Denise Child 

    This is the code

    $x=import-csv C:\Users\f.malaeb\test1.cvs
    #Clear
    $x.ForEach({Set-ADUser $_.sAMAccountName -Clear Info,ipphone})
    
    #Replace
    $x.ForEach({Set-ADUser $_.sAMAccountName -Replace @{info=$_.info;ipPhone=$_.ipPhone}})
  • farismalaeb's avatar
    farismalaeb
    Steel Contributor

    Denise Child 

    This is the code

    $x=import-csv C:\Users\f.malaeb\test1.cvs
    #Clear
    $x.ForEach({Set-ADUser $_.sAMAccountName -Clear Info,ipphone})
    
    #Replace
    $x.ForEach({Set-ADUser $_.sAMAccountName -Replace @{info=$_.info;ipPhone=$_.ipPhone}})
    • Denise Child's avatar
      Denise Child
      Iron Contributor
      Thank you for the quick response farismalaeb
      This helps a lot...I was struggling on how to write this.
      I updated the code since the Replace gave an error.

      This one works even though it still throws an error.
      #Replace
      $x.ForEach({Set-ADUser $_.sAMAccountName -Replace @{info=$_.info;};@{ipPhone=$_.ipPhone}})

      Some of my test users had the fields blank and others were filled out. Probably needs error checking?

      Here is the error message:
      Set-ADUser : replace
      At C:\Temp\ADUser_Info_ipPhone_change.ps1:6 char:13
      + $x.ForEach({Set-ADUser $_.sAMAccountName -Replace @{info=$_.info;};@{ ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (dc:ADUser) [Set-ADUser], ADInvalidOperationException
      + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

      • farismalaeb's avatar
        farismalaeb
        Steel Contributor
        Can you tell if the error for users with blank fields or not.
        I my test I had 2 users only

Resources