Last active
August 3, 2022 20:32
-
-
Save wise-io/8b97ea4023d7799b1ef610ee18b15cbd to your computer and use it in GitHub Desktop.
Manages members of the local Administrators security group with PowerShell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string[]]$Admins = @() | |
) | |
$Admins += @( | |
# Enter your admin users here as follows: | |
# "$env:computername\AdminUser1", | |
# 'DOMAIN\AdminUser2', | |
# 'AzureAD\AdminUser3' | |
) | |
# Get current administrator group members | |
$AdminGroup = Get-LocalGroupMember -Group 'Administrators' | |
# Add admin users | |
ForEach ($User in $Admins) { | |
if ($AdminGroup.Name -notcontains $User) { | |
Write-Output "Adding $User to Local Administrators group..." | |
Add-LocalGroupMember -Group 'Administrators' -Member $User | |
} | |
} | |
# Remove non-admin users | |
ForEach ($Member in $AdminGroup) { | |
if ($Admins -notcontains $Member) { | |
Write-Output "Removing $Member from Administrators group..." | |
Remove-LocalGroupMember -Group 'Administrators' -Member $Member | |
} | |
} | |
# Display current administrators group members | |
Write-Output "`nCurrent Administrators Group Members:" | |
Get-LocalGroupMember -Group 'Administrators' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment