Skip to content

Instantly share code, notes, and snippets.

@wise-io
Last active August 3, 2022 20:32
Show Gist options
  • Select an option

  • Save wise-io/8b97ea4023d7799b1ef610ee18b15cbd to your computer and use it in GitHub Desktop.

Select an option

Save wise-io/8b97ea4023d7799b1ef610ee18b15cbd to your computer and use it in GitHub Desktop.

Revisions

  1. Aaron Stevenson revised this gist Jun 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ManageLocalAdmins.ps1
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ param(

    $Admins += @(
    # Enter your admin users here as follows:
    # '$env:computername\AdminUser1',
    # "$env:computername\AdminUser1",
    # 'DOMAIN\AdminUser2',
    # 'AzureAD\AdminUser3'
    )
  2. Aaron Stevenson created this gist Jun 21, 2022.
    33 changes: 33 additions & 0 deletions ManageLocalAdmins.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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'