Verify all AD groups added to a domain user

Most important task when you join a new organization or when you start working for a new client is to gain access to all required AD groups. I had to switch several times and this was a pain point. My manager would share a list of AD groups and ask me to check if my account is added to all those groups across all domains. Hence being lazy, I had to make use of below simple script to fetch and compare lists quickly at each domain.

##*------------------------------------------------------------------------------------------------------------------------------------------
#  Filename       : groups.ps1
#  Purpose        : Verify if all AD groups are added for a domain user account
#  Schedule       : NONE
#  Date           : 05-March-2018
#  Author         : www.sherbaz.com/Sherbaz Mohamed
#  Version        : 1
#  OS             : Windows Server 2012
#
#  Important --arks:	
#  INPUT          : Username, File path (for AD groups)
#  VARIABLE       : NONE
#  PARENT         : NONE
#  CHILD          : NONE
#  NOTE           : Arguments are mandatory. The input text file for argument 2 should contain the list of AD groups.
#---------------------------------------------------------------------------------------------------------------------------------------------
# Usage:
# .\groups.ps1 -username sherbaz -groupsfile .\groups.txt
#
##*-------------------------------------------------------------------------------------------------------------------------------------------*/

Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$username,
    [Parameter(Mandatory=$True)]
    [string]$groupsfile
)


$memberships = dsquery user -name $username | dsget user -memberof

"Below groups were missing"

foreach($group in Get-Content $groupsfile)
{
    $found = $memberships | where {$_ -Match "$group"}
    if($found -eq $null) {"Not Found: $group"}
}

Store the script in a file and name it for example “groups.ps1”. Copy-paste all AD group names required for your job role into a separate text file named groups.txt in the same folder. Open a powershell prompt, Navigate into the directory and execute below command.

.\groups.ps1 -username sherbaz -groupsfile .\groups.txt

You could further customize the script based on your requirement.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.