Set-ADUser in multiple domains

Welcome Forums General PowerShell Q&A Set-ADUser in multiple domains

Viewing 3 reply threads
  • Author
    Posts
    • #211323
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      Hi Guys,

      I have a csv which contains UPNs for users which are within multiple domains in our forest. I need to clear the description for these accounts but for the life of me, I cannot get the script I have to work!

      Current script

      $Users = Import-csv C:\Temp\Users.csv
      foreach($User in $Users){
      Set-ADUser -server dc01.domain.co.uk:3268 $User.UserPrincipalName -Description $Null
      }
      • This topic was modified 2 months, 1 week ago by REAFC46.
    • #211353
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Please format it as code using the code tags “PRE“. Thanks
      You might read the instructions how to use this forum: Read Me Before Posting! You’ll be Glad You Did!

      Try parsing the domain from the UPN assuming that the UPN is the domain name and not a mail domain:

      #Emulate Import-csv C:\Temp\Users.csv
      $Users = @"
      jsmith@domainx.com
      sjohnson@domainy.com
      "@ | ConvertFrom-Csv -Header UserPrincipalName
      
      foreach($User in $Users){
          $userDomain = ($user.UserPrincipalName -split '@')[1]
      
          'Connecting to {0} on domain {1}' -f $user.UserPrincipalName, $userDomain
          Set-ADUser -Identity $User.UserPrincipalName -Server $userDomain -Clear Description -WhatIf
      }
      

      Also, to clear a attribute value, you should use the -Clear parameter, not set the value to NULL manually.

      • This reply was modified 2 months, 1 week ago by Rob Simmers.
    • #211365
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      Thanks, I’ve amended the post.

      The domain name is a mix of the mail domain and the domain name.

      The error I get is

      “Set-ADUser : Cannot find an object with identity”.

      I think the command cannot find the users in their domains.

       

      • This reply was modified 2 months, 1 week ago by REAFC46.
    • #211551
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      We are at a bit of impasse as you are saying…

      The domain name is a mix of the mail domain and the domain name.

      But providing no examples of a upn and what you are trying to extact. The basic idea is if you can extract that name from the UPN and it is resolvable in DNS, then you can use it as the -Server param and do the lookup in the proper domain. If DNS will not work, you would need to manually define DNS resolvable domain controllers to do the lookups.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.