Skip to content

Instantly share code, notes, and snippets.

@1951FDG
Last active May 15, 2018 12:24
Show Gist options
  • Save 1951FDG/15f8de2013af27abba965d5ee1477edf to your computer and use it in GitHub Desktop.
Save 1951FDG/15f8de2013af27abba965d5ee1477edf to your computer and use it in GitHub Desktop.
Param (
[string]$oldDomain = "{OLDDOMAIN}",
[string]$newDomain = "{NEWDOMAIN}",
[string]$SearchBase = "{SEARCHBASE}"
)
Write-Verbose "$(Get-Date): Loading ActiveDirectory module..."
Try { Import-Module ActiveDirectory -ErrorAction Stop }
Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?" -ForegroundColor Red; Exit }
Get-ADUser -Filter * -Properties givenName, Surname, EmailAddress, sAMAccountName -searchbase $SearchBase | %{
if ($_.SurName -eq $null)
{
Write-Host "User Name:" $($_.sAMAccountName) "has been not been updated" -ForegroundColor Yellow
}
else
{
if ($_.EmailAddress)
{
if ($_.EmailAddress.EndsWith($oldDomain))
{
#Write-Output $($_.EmailAddress).Replace($oldDomain, $newDomain)
Set-ADUser -Identity $_ -EmailAddress $($_.EmailAddress).Replace($oldDomain, $newDomain)
Write-Host "User Name:" $($_.sAMAccountName) "has been updated" -ForegroundColor Green
}
}
else
{
Write-Host "User Name:" $($_.sAMAccountName) "has not been updated" -ForegroundColor Red
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment