Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Issue with Name Change Script

$
0
0

Hi All,

We are looking to change everyone's username in bulk in AD using a script. Currently the usernames are (lastname)(firstinitial) e.g. smithj to (firstname).(lastname). e.g. john.smith

So here's what I have but its having an issue figuring out the user name from the variable $name. If anyone can figure it out that would be great, hopefully something easy. Thanks.

 

$ErrorActionPreference = "SilentlyContinue"

 

#Imports AD module...

Import-Module ActiveDirectory

$searchOU = "OU=Script Test,OU=District,DC=***,DC=*****"

 

#Sets a variable to users which is all users found in the specified OU and their home directory properties...

$users = Get-ADuser -filter * -SearchBase $searchOU

 

#Goes through a for each loop to process new username for each user...

    foreach ($user in $users) {

$sam = (Get-Aduser -identity $user).sAMAccountName    

$gn = (Get-Aduser -identity $user).givenName 

$sn = (Get-Aduser -identity $user).surName 

#$name = "$gn.$sn"

    Set-ADUser -sAMAccountName $name

    Set-ADUser -userprincipalname $name

$email = $name + '@colchestersd.org'

    Set-ADUser -Mail $email $name

Write-Host Changed user logon name to $user.userPrincipalName

Write-Host Changed user logon name (pre-Windows 2000) to $user.sAMAccountName

Write-Host Changed user email to $user.mail

}

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles