How to disable an AD user powershell Automation

Опубликовано: 06 Октябрь 2024
на канале: Fun & Tech
681
10

Automate with an AD user account name and disable the account, move to an OU, clear some of the attributes, delete the user from all the member of. Save the script as a ps1 file and run it in AD.

script:
$username = Read-Host -prompt 'Enter the user account you want to disable'

Write-Output ***********************************
Write-Host 'Before disabling the user account' -ForegroundColor green
Write-Output ***********************************
#Write-host "Press to continue"
get-aduser -identity $username -properties department, mail, title, description
$disable = Get-ADUser -identity $username

disable-adaccount -identity $disable

move-adobject -identity $disable -targetpath "OU=Disabled, DC=windc, DC=local"


Get-AdPrincipalGroupMembership -Identity $username | Where-Object -Property Name -Ne -Value 'Domain Users' | Remove-AdGroupMember -Members $username -Confirm:$false

set-aduser -identity $username -clear department, mail, title, description
Write-Output **********************************
Write-Host 'After disabling the user account' -ForegroundColor red
Write-Output **********************************
get-aduser -identity $username -properties department, mail, title, description