Check If AD User Is Member Of Group

This is my last Blogpost of the Year 2020, the year has not gone well for many of us, some have lost their jobs, and others have lost their loved ones because of COVID 19 pandemic.

We are praying that New Year will start mitigating the sufferings and will bring light in everyone’s life.

Today’s post is about the Active Directory function that I wanted to make and share with the community.

This New function came in my mind when I had written the Get-ADGroupMembersRecursive
and it is a success among the community.

We always get a situation on daily basis to check if user is member of group or not and we use some script or some other way to check.

Why not we wrap the script in a easy to use function same as Quest Module which has Get-QadMemberOf.

Our Function Name is: Get-ADUserMemberOf

How it works?

Get-ADUserMemberOf -User “User” -Group “Group”

It will return True if user is member of group and will return False if user is not member of the group.

If you will enter wrong values, it will not return anything. (you can check the error in $error variable if you want)


 



This function is now part of vsadmin module

Here is the function code:

Function Get-ADUserMemberOf
{
  Param(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]$User,
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]$Group
  )
  try{
    $GroupDN = (Get-ADGroup $Group).DistinguishedName
    $UserDN = (Get-ADUser $User).DistinguishedName
    $Getaduser = Get-ADUser -Filter "memberOf -RecursiveMatch '$GroupDN'" -SearchBase $UserDN
    If($Getaduser) {
      $true
    }
    Else { 
      $false 
    }
  }
  catch{

  }
} #Get-ADUserMemberOf

 

Thanks for reading and utilizing…

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/

PowerShell Cheat Book

One thought on “Check If AD User Is Member Of Group

  1. Pingback: PowerShell System Admin Module | Tech Wizard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s