script to find users from multiple security groups

Hi team,

I have about 300 security groups that I need to get the members of into a csv/xls.

All the group names contain the words "VPN User".

I need to get membership for all such group, so is there some type of command or power

April 23rd, 2015 10:21am

Hi Triyambak,

where there is a will, there is a script:

Get-ADGroup -Filter { name -like "*VPN User*" } | Get-ADGroupMember | Export-Csv "members.csv"

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 10:31am

Try something like this

Get-ADGroup -Filter {Name -like '*VPN User*'} | Get-ADGroupMember | Export-Csv C:\GroupMembers.csv
EDIT: Looks like Fred beat me
April 23rd, 2015 10:31am

Script terminated with below error. output file show username with samid but not their respective

group name


Get-ADGroupMember : The size limit for this request was exceeded
At line:1 char:70

Free Windows Admin Tool Kit Click here and download it now
April 24th, 2015 6:31am

$Groups = Get-ADGroup -filter {name -like "*VPN User*"} -properties description,members 
$myCol = @()
ForEach ($Group in $Groups)
{
	$Members = @(Get-ADGroupMember "$Group")
	If($Members.count eq 0)
     {
	  $MyObject = New-Object PSObject -Property @{
	    Group = $Group.name
        OUPath = $group.DistinguishedName -replace '^.+?,(CN|OU.+),$1'
	    Type = $group.groupcategory
        ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
        Members = "This group is empty"
	    UserEnabled = ""
	    }
	 $mycol += $MyObject
     }
	Else
     {
	    ForEach ($Member in $Members)
         {
		  try
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OUPath = $group.DistinguishedName -replace '^.+?,(CN|OU.+),$1'
		    Type = $group.groupcategory
		    Members = $member.name
		    UserEnabled = (get-aduser $member.name).Enabled
		    }
		    $mycol += $MyObject
           }
		catch
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OUPath = $group.DistinguishedName -replace '^.+?,(CN|OU.+),$1'
		    Type = $group.groupcategory
		    Members = $member.name
		    UserEnabled = ""
		    }
		    $mycol += $MyObject
           }
		}
	 }
}
$myCol |select group,OUPath,type,members,userenabled | export-csv membersingroups.csv -Delimiter ";" -Encoding UTF8 -NoTypeInformation

  • Proposed as answer by Mekac 20 hours 5 minutes ago
April 24th, 2015 6:52am

$Groups = Get-ADGroup -filter {name -like "*VPN User*"} -properties description,members 
$myCol = @()
ForEach ($Group in $Groups)
{
	$Members = @(Get-ADGroupMember "$Group")
	If($Members.count eq 0)
     {
	  $MyObject = New-Object PSObject -Property @{
	    Group = $Group.name
        OUPath = $group.DistinguishedName -replace '^.+?,(CN|OU.+),$1'
	    Type = $group.groupcategory
        ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
        Members = "This group is empty"
	    UserEnabled = ""
	    }
	 $mycol += $MyObject
     }
	Else
     {
	    ForEach ($Member in $Members)
         {
		  try
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OUPath = $group.DistinguishedName -replace '^.+?,(CN|OU.+),$1'
		    Type = $group.groupcategory
		    Members = $member.name
		    UserEnabled = (get-aduser $member.name).Enabled
		    }
		    $mycol += $MyObject
           }
		catch
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OUPath = $group.DistinguishedName -replace '^.+?,(CN|OU.+),$1'
		    Type = $group.groupcategory
		    Members = $member.name
		    UserEnabled = ""
		    }
		    $mycol += $MyObject
           }
		}
	 }
}
$myCol |select group,OUPath,type,members,userenabled | export-csv membersingroups.csv -Delimiter ";" -Encoding UTF8 -NoTypeInformation

  • Proposed as answer by Mekac Friday, April 24, 2015 11:03 AM
Free Windows Admin Tool Kit Click here and download it now
April 24th, 2015 10:51am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics