Now in this instead of putting all groups starting with DLP I want to put all groups starting with DLP_ except one DLP_MOD_USB. Is there a better way of doing this?
Thanks Olaf for your help. The change seems to work except for one part. I get a list of users who are member of DLP_MOD_USB and DLP_PHONE, I should be getting list of only users who are members of DLP_MOD_USB. Any clue what may be wrong. Mind you the same seems to be the case before the change as well.
Ok let me explain again :)). From the AD search criteria i have selected I need users who are members of only and only DLP_MOD_USB not member of any other DLP_ group.
I am confused on what your are looking for so I provided two options.
#Option 1
# Get DLP groups except DLP_MOD_USB group first then get list of users from each group
$sammatch = '^co-|^ea-|^em-|^gl-|^hi-|^lo-|^ne-|^ni-|^nw-|^se-|^wa-|^wm-|^wx-|^yh-'
$sbase = "OU=MyDomain Users and Computers,DC=My,DC=domain,DC=in"
Get-ADGroup -Filter {Name -like 'DLP_*' -AND Name -ne 'DLP_MOD_USB'} -SearchBase $sbase |
Get-ADGroupMember | Where-Object {
$_.SamAccountName -match $sammatch -AND $_.ObjectClass -eq 'user'} |
Get-ADUser -Properties MemberOf
#Option 2
# Get list of users in group DLP_MOD_USB ONLY
$sammatch = '^co-|^ea-|^em-|^gl-|^hi-|^lo-|^ne-|^ni-|^nw-|^se-|^wa-|^wm-|^wx-|^yh-'
$sbase = "OU=MyDomain Users and Computers,DC=My,DC=domain,DC=in"
Get-ADGroup -Identity 'DLP_MOD_USB' -SearchBase $sbase |
Get-ADGroupMember | Where-Object {
$_.SamAccountName -match $sammatch -AND $_.ObjectClass -eq 'user'} |
Get-ADUser -Properties MemberOf