tools/Remove-DisabledUsers.ps1
function Remove-DisabledUsers { <# .SYNOPSIS This function checks a specified OU for disabled users and removes them from AD It will also delete their home folder and roaming profile folder .NOTES Name: Remove-DisabledUsers Author: Elliott Marter .EXAMPLE Get-Something -UserPrincipalName "username@thesysadminchannel.com" .LINK https://www.powershellgallery.com/profiles/elliottmarter #> [cmdletbinding(SupportsShouldProcess=$True)] $OU = (Select-ADOrganizationalUnit).DistinguishedName # Get all disabled users in OU $Users = Get-ADUser -Filter {Enabled -eq $false} -Properties * -SearchBase $OU if ($Users -eq $null) {throw "No users to remove in $OU"} $Users | Format-Table Name,samaccountname # Here is a confirmation that exits unless y is entered $Confirm = Read-Host "Proceed with the removal? [y/n]" if ($Confirm -ne "y") {throw "Removal aborted..."} foreach ($U in $Users) { Write-Host "Removing $($U.samaccountname) Home Folder - $($U.homedirectory)" -ForegroundColor Cyan Remove-Item -Path $U.homedirectory -Recurse -Force Write-Host "Removing $($U.samaccountname) Profile Folder - $($U.profilepath)" -ForegroundColor Cyan Remove-Item -Path "$($U.profilepath).V*" -Recurse -Force Write-Host "Removing $($U.samaccountname) Account" -ForegroundColor Cyan Remove-ADUser $U.samaccountname -Confirm:$false Write-Host "$("*" * 20)" } } |