I review for BookLook Bloggers

Wednesday, August 22, 2018

Fix AD-USERS Naming Convention [1st letter Uppercase and rest Lower]

Description
Description: This Script Does the following things.
1> Rename Active Directory User's Firstname, Lastname 1st letter to Uppercase ex: JOhn SnOw or john snOn or JOHN SNOW becomes -, John Snow
2> Rename Active Directory User's samaccountname to all lower case example: Jonh becomes - john
3> Rename Active Directory User's Displayname and NAme 1st letter to Uppercase ex: JOhn SnOw or john snOn or JOHN SNOW becomes -John Snow

Source Code

** Recommend to perform in the test environment before moving to the production environment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#===========================================================================================================================
Script Name: FixADNAmes.ps1
Description: This Script Does the following things.
            1> Rename Active Directory User's  Firstname,Lastname 1st letter to Uppercase ex: JOhn SnOw or john snOn or JOHN SNOW becomes -John Snow
            2> Rename Active Directory User's samaccountname to all lower case example: Jonh becomes - john
            3> Rename Active Directory User's  Displayname and NAme 1st letter to Uppercase ex: JOhn SnOw or john snOn or JOHN SNOW becomes -John Snow

           
      Inputs: OU path if Required to run on an OU
      Outputs: 

Notes: Run as administrator
Date Created: 08/21/2018
      Credits: 
Last Revised: 08/21/2018


** Instructions**
Just Launch Powershell as administrator copy the script and run or save and run it as
FixADNAmes.ps1
=============================================================================================================================#> 
If ( ! (Get-module ActiveDirectory )) {
  Import-Module ActiveDirectory
  Clear-Host
  }

Get-ADUser -filter *  <#-SearchBase 'OU=OU,DC=domain,DC=Local'#>|Foreach{
$newFname=(Get-Culture).textinfo.totitlecase(($_.givenname).tolower())
$newLname=(Get-Culture).textinfo.totitlecase(($_.Surname).tolower())
$name= $newFname + ' ' + $newLname
Set-ADUser $_ -GivenName $newFname -Surname $newLname
Rename-ADObject -Id $_ -NewName $name
Set-ADUser $_.samaccountname -DisplayName $name
Set-ADUser $_.samaccountname -SamAccountName $_.SamAccountName.ToLower()
}



Clear-Host
Write-Host 'The process has been completed' -ForegroundColor Yellow
"Here is how AD users Looks now"
$newU=(Get-ADUser -filter * <#-SearchBase $ou #> -prop Displayname |Get-Random |select -First 1)
$newU |select Name,Displayname,Givenname,surname,samaccountname

Screenshots