How to use Powershell script to automatically remove the group of outgoing users and save the log
Recently, the work received a requirement to clean up the group and log the account of the departing personnel, so I studied how to use Powershell to achieve this function:
# query the exit account under the corresponding OU
$users = get-aduser-Filter *-SearchBase "OU=xxxx,DC=it581,DC=com" | foreach {if ($.enabled-eq $false) {echo $.sampled name}}
# get the current date
$data=get-date-Format 'yyyyMMdd'
# use a circular log and delete the corresponding group
Foreach ($user in $users)
{
Get-ADUser-Identity $user-Properties * | select name,memberof | fl > > d:\ $data.txt
Get-ADPrincipalGroupMembership-Identity $user | where {$.name-notlike "domain users"} |% {Remove-ADPrincipalGroupMembership-Identity $user-MemberOf $- Confirm:$false-ErrorAction SilentlyContinue}
}
Write-host "removal complete!"
The above is the script that implements this requirement.