Deprovision User Accounts

#Purpose: Fully deprovision users being offboarded and add notes to telephone tab in AD

$csvPath = "C:\Users\Public\Documents\Terminations.csv"

 

$targetOU = "OU=<obfuscated>,OU=<obfuscated>,DC=<obfuscated>,DC=<obfuscated>,DC=<obfuscated>"

 

$users = Import-Csv -Path $csvPath

 

foreach ($userRecord in $users) {

        $user = $userRecord.Username

        $newNote = $userRecord.NewNote

 

        Disable-ADAccount -Identity $user

 

        $groups = Get-ADPrincipalGroupMembership -Identity $user

 

        foreach ($group in $groups) {

                    Remove-ADGroupMember -Identity $group -Members $user -Confirm:$false

        }

 

        $currentNotes = (Get-ADUser $user -Properties info).info

 

        $updatedNotes = "$currentNotes`r`n$newNote"

 

        Set-ADUser $user -Replace @{info = $updatedNotes}

 

Move-ADObject -Identity (Get-ADUser $user).DistinguishedName -TargetPath $targetOU

 

        Write-Output "Deprovision process completed for $user"

}