New to Powershell here. I created the script below from a combination of several different scripts I found on the internet. We use the script for disabling accounts when we have employee separations at our company. In addition to disabling the account it also makes some changes to the user's account information in Exchange and AD.
You will notice I had to call the AD module at the end in order to handle the iPPhone value since that is not a value that can be modified through exchange.
You will also notice that the second line of the script prompts the user for the account Alias stored in value $alias. I tried to use the same $alias variable later in the script during the AD portion but for some reason it prompts me again for "Identity" and i have to put the alias in again to continue the script. Ideally I would like to only have to type the alias in one time and have it apply to the entire script. Any ideas?
#Set $alias to whatever account you want to modify
$alias = Read-Host 'Account Alias?'
#Get today's date for the description field
$date = Get-Date -Format g
#Hide account from GAL and delete all user info
Set-Mailbox -HiddenFromAddressListsEnabled $true -Identity $alias
Set-User -Phone '' -Pager '' -Fax '' -HomePhone '' -MobilePhone '' -Identity $alias
#Get AD Module
Import-Module ActiveDirectory
#Remove all groups except "Domain Users" from the user account
Get-ADPrincipalGroupMembership -Identity $alias | Where-Object {$_ -notmatch "CN=Domain Users,CN=Users,DC=domainname,DC=local"} | % {Remove-ADPrincipalGroupMembership -Identity $alias -MemberOf $_ -confirm:$false}
#Clear ipPhone attribute
$user = Get-ADUser $alias -Properties ipPhone
$user.ipPhone = $null
Set-ADUser -Instance $user
#Add today's date to description
$date = Get-Date -Format g
$disabledMsg = "Disabled on " + $date
Set-ADUser -Description $disabledMsg
#Disable and move to Disabled Users OU
Get-ADUser -Identity $alias | Disable-ADAccount
Get-ADUser -Identity $alias | Move-ADObject -TargetPath "OU=Users,OU=Disabled,DC=domainname,DC=local" -PassThruOur Mission is to Honor and Empower Wounded Warriors...