List All Security Groups and their members including any sub groups

Hi, I have the basic command below I've pieced together but I want the script to just List the Group name and each user for every security group that exists in a CSV file, also please confirm how to run the script if its different to the below.

Import-module activedirectory

get-adgroupmember "GROUP NAME" -recursive  export-csv 'c:\GroupMemberships.csv' -notypeinformation

October 28th, 2013 12:26pm

I suggest searching the forum. There are plenty of threads like this with example code.
Free Windows Admin Tool Kit Click here and download it now
October 28th, 2013 12:31pm

Hi, I did find the below script, could you confirm how I run it, I will call the .ps1 file from poweshell but need to know what I need to change if anything for this to work as it does not currently run:


Import-module activedirectory
$ou =OU=Groups,DC=domain,DC=com"
Get-ADGroup -Filter * -SearchBase $OU | select -ExpandProperty name | % {
$group= "$_"
$result += Get-ADGroupMember -identity "$_" | select @{n="Group";e={$group}},Name 
}
$result | export-csv 'membership.csv' -notypeinformation

October 28th, 2013 12:46pm

What's the error message you're getting? You'll probably need to set the execution policy before you can run your script:

http://ss64.com/ps/set-executionpolicy.html

I use RemoteSigned.

Free Windows Admin Tool Kit Click here and download it now
October 28th, 2013 1:30pm

PS C:\> c:\GetAllSecGroupmembers.ps1 The string starting: At C:\GetAllSecGroupmembers.ps1:4 char:64 + $result += Get-ADGroupMember -identity "$_" | select @{n="Group <<<< ";e={$gr oup}},Name is missing the terminator: ". At C:\GetAllSecGroupmembers.ps1:6 char:60 + $result | export-csv 'c:\membership.csv' -notypeinformation <<<< + CategoryInfo : ParserError: (;e={$group}},Na...typeinformation: String) [], ParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

That was the first issue, but there is something missing in the code causing the above error.

October 29th, 2013 5:01am

I think the quote after $ou =OU=Groups,DC=domain,DC=com should not have been there.

I removed it and now receive the below error using the below code:

$ou =OU=Groups,DC=domain,DC=com
Get-ADGroup -Filter * -SearchBase $OU | select -ExpandProperty name | % {
$group= "$_"
$result += Get-ADGroupMember -identity "$_" | select @{n="Group";e={$group}},Name 
}
$result | export-csv 'c:\membership.csv' -notypeinformation
Error is:
Get-ADGroup : Cannot validate argument on parameter 'SearchBase'. The argument
is null. Supply a non-null argument and try the command again.
At line:1 char:34


Free Windows Admin Tool Kit Click here and download it now
October 29th, 2013 6:52am

The $ou variable should be a string, as that's what SearchBase is expecting.

Try:

Replace:
$ou =OU=Groups,DC=domain,DC=com


With:
$ou = 'OU=Groups,DC=domain,DC=com'		
October 29th, 2013 9:18am

Hi,

To export all members and the groups of OU, the script below is helpful for you:

Get-ADGroup -properties * -filter * -searchbase "OU=Groups,DC=domain,DC=com" |
ForEach-Object{
$hash=@{GroupName=$_.Name;Member=''}
$_ | Get-ADGroupMember -ea 0 -recurs |
ForEach-Object{
$hash.Member=$_.Name
New-Object psObject -Property $hash
}
} | export-csv E:\securitygroups.csv

I hope t
Free Windows Admin Tool Kit Click here and download it now
October 29th, 2013 10:04pm

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

Other recent topics Other recent topics