Share via

Powershell to modify user object

Eaven HUANG 2,196 Reputation points
2022-08-11T02:28:31.383+00:00

Dear experts,

I'm looking for a script to do the following tasks:

  1. Get the OU of this user, not the whole OU property name but only the OU name where it belongs to. e.g. CN=Eaven.test,OU=IT Test,OU=Admins,OU=Staff,OU=xxx, but I need only IT Test information
  2. Get user employeeID
  3. Update Description as "(employeeID) SPACE OU name"
  4. Add a few groups for the user
  5. Update employeeType attribute

Any idea would be much appreciated.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
    2022-08-11T06:38:24.15+00:00

    Hi @Eaven HUANG ,

    please try this script if it fits your requirements:

    $Username = "mmouse"  
    $groups = "Group1","Group2"  
    $DN = ((Get-ADUser -Identity $Username).DistinguishedName).Split(",",2)[1]  
    $OUname = Get-ADOrganizationalUnit -Identity $DN -Properties * | select name  
    $employeeID = ((Get-ADUser -Identity $Username -Properties *).EmployeeID)  
    Set-ADUser $Username -Description (($employeeID) + " " + $($OUname.Name))  
    $groups | ForEach-Object {Add-ADPrincipalGroupMembership (Get-ADUser -Identity $Username) -MemberOf $_}  
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.