Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Powershell script to automatically remove the group of outgoing users and save the log

Shulou Source: shulou.com Published: 2022-06-02 01:12:52 09月13日 Update

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.

Tags: Logs accounts requirements ownership scripts people functions that is dates faces jobs loops queries research users Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Xiaomi vpn Redmi