Posted On July 15, 2019

PowerShell: check whether the current user is a member of Domain Admins

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: check whether the current user is a member of Domain Admins
# short snippet to check whether the currently login user is a domain admin
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$WindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($CurrentUser)
if($WindowsPrincipal.IsInRole("Domain Admins")){
Write-Host "$($currentUser.Name) is a Domain Admin"
}else{
Write-Host "$($currentUser.Name) not a Domain Admin."
}
# 1-liner to check members of the "Domain Admins" Group
Get-ADGroupMember -Identity "Domain Admins" -Recursive | %{Get-ADUser -Identity $_.distinguishedName} | Where-Object {$_.Enabled -eq $True} | Select Name
# 1-liner to check members of the "Enterprise Admins" Group
Get-ADGroupMember -Identity "Enterprise Admins" -Recursive | %{Get-ADUser -Identity $_.distinguishedName} | Where-Object {$_.Enabled -eq $True} | Select Name
# Function to check whether a given username matches the list of Domain Admins
function validateDomainAdmin{
param (
[string]$username
)
$domainAdmins=Get-ADGroupMember -Identity "Domain Admins" -Recursive | %{Get-ADUser -Identity $_.distinguishedName} | Where-Object {$_.Enabled -eq $True}
$matchedAdmin=$username -in $domainAdmins.SamAccountName
if($matchedAdmin){
Write-Host "$username is a Domain Admin"
}else{
Write-Host "$username not a Domain Admin."
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Fix Email Aliases as Preparation for O365 Migration

Check SMTP Addresses of On Premise Mailboxes function checkSmtpAddresses{ $microsoftDomain1="@kimconnect.mail.onmicrosoft.com" $microsoftDomain2="@kimconnect.onmicrosoft.com" $dotLocal=".local" $onpremMailboxes=Get-Mailbox -ResultSize unlimited…

PowerShell: Use EMCOPY to Mirror a Directory

# Purpose: this PowerShell snippet is to demonstrate the use of Emcopy$source="C:\Users\tester\Desktop\Clients"$destination="C:\Users\tester\Desktop\Test"#$switches="/o /secforce /s /de…

WordPress: Add Search Box Into Header

Navigate to Appearance > Theme Editor > select header.php > search for this section: <div…