#Add exchange 2010 snap in for mailbox creation
Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010 -ea silentlyContinue
#Grabs username from command line to use in deleting
$username = $args[0]
#Set domain
$domain = "dom1.now"
#Setup share information
$fileserver = "Alexandria"
$share = "$username$"
#Setup paths to user folders
$homedrivepath = "\\alexandria\d$\users\" + $username
$profilepath = "\\alexandria\d$\profiles\" + $username
if ($username -eq $null){
Write-Host "No user specified"
Write-Host "Script format is: "
Write-Host "-----------------"
Write-Host ".\DeleteStaff.ps1 username"
}
else{
#Delete user account and mailbox
Write-Host "Deleting $username's mailbox and user account"
Remove-Mailbox -Identity $username -Permanent $true
#Remove Share
Write-Host "Removing users homefolder share"
(Get-WmiObject -Class Win32_Share -ComputerName $fileserver -Filter "Name='$share'").InvokeMethod("Delete",$null) | Out-Null
#Remove Homedrive folder
Write-Host "Removing home drive folder"
Remove-Item $homedrivepath -recurse
#Remove Profile folder
Write-Host "Removing Profile folder"
Remove-Item $profilepath -recurse
#Replicate Changes to other DCs
Write-Host "Replicating user deletion to all domain controllers"
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain","$domain")
$dclist = [System.DirectoryServices.ActiveDirectory.DomainController]::findall($context)
foreach($dc in $dclist)
{
$dc.SyncReplicaFromAllServers(([ADSI]"").distinguishedName,'CrossSite')
}
Write-Host "User $username deleted"
}
|