PoshCode Logo PowerShell Code Repository

Test-GroupMembership by Mike Pfeiffer 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1397"></script>download | new post

Checks whether a user is a member of a specified AD group. Requires Quest.ActiveRoles.ADManagement snapin.
Usage: Test-GroupMembership username groupname

  1. Function Test-GroupMembership {
  2.         Param($user,$group)
  3.         Get-QADUser $user | select memberof | %{
  4.                 $result = $false
  5.                 foreach ($i in $_.memberof) {
  6.                         if ((Get-QADGroup $i).name -match $group){
  7.                                 $result = $true
  8.                                 break
  9.                         }
  10.                 }
  11.                 $obj = "" | select Name,Group,IsMember
  12.                 $obj.Name = $user
  13.                 $obj.Group = $group
  14.                 $obj.IsMember = $result
  15.                 write $obj
  16.         }
  17. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me