Skip to content

Instantly share code, notes, and snippets.

@Agazoth
Created May 15, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Agazoth/e74eeda8e31008658d8c6f3728fdd432 to your computer and use it in GitHub Desktop.
Save Agazoth/e74eeda8e31008658d8c6f3728fdd432 to your computer and use it in GitHub Desktop.
RemoveAdminCount
function Remove-AdminCount
{
[CmdletBinding()]
    param ($Samaccountname)
    if (!$(Get-Module activedirectory)){import-module activedirectory}
try
    {
        $User = Get-ADUser $Samaccountname -Properties Admincount
        if ($User.Admincount -eq 1)
        {
            Write-Verbose "Admin Count found on $Samaccountname"
            set-aduser $Samaccountname -Replace @{admincount=0}
            $command = 'dsacls "' + $user.distinguishedName + '" /P:N'
            iex $command > $null
            Write-Verbose "Admin Count removed from $Samaccountname"
        }
        else {Write-Verbose "Admincount not set on $Samaccountname"}
    }
    catch {Write-Warning "$Samaccountname does not exist in AD"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment