Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Powershell script that can query a OU and get all group name,Members and count of members into a csv/Excel.

Hi,

Powershell script that can query a OU and get all group name,Members and count of members into a csv/Excel.
Only Distribution groups.

Can anyone help me with a code that can do this.

REgards
Sharath
Avatar of Lee Osborne
Lee Osborne
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you tried PowerGUI? http://www.powergui.org/index.jspa

This should help you do exactly what you want to do, and more!

Lee
Avatar of bsharath

ASKER

thanks but how can i get the counts of each group members
try this:


$LDAP = "LDAP://OU=myOU,DC=myDomain,DC=MySuffx"
$RootPath = [ADSI] $LDAP
$groups = $RootPath.PSBase.Get_Children() | Where-Object {$_.Class -eq "Group"}
foreach ($group in $Groups){Write-host $group.Name"`,"$group.member.count"`,"$group.member}
X-men is this powershell
yes
here is the entire script (powershell)

Edit Row1 with your LDAP path

the result will be writen in the file "c:\ee.csv" (line 16)


$LDAP = "LDAP://OU=myOU,DC=MyDomain,DC=org"
$RootPath = [ADSI] $LDAP
$groups = $RootPath.PSBase.Get_Children() | Where-Object {$_.Class -eq "Group"}
$xTable = @()
foreach ($group in $Groups)
    {$o="" | select GName,GMembers,GCount; 
    $o.GName=$group.Name.ToString();
    $o.GMembers=$group.member.ToString(); 
    $o.GCount=$group.member.count; 
    $xTable += $o}
If ($xTable.Length -ne 0)
    {foreach ($x in $xTable)
        {$xGName=$x.GName;
        $xGMembers=$x.GMembers;
        $xGCount=$x.GCount
        Add-Content C:\ee.csv -Value "$xGName`,$xGMembers`,$xGCount"}
    }

Open in new window

Thanks
I get each portion in  each cell and its difficult to read or sort

OU=Named Users      OU=Users

Can i get just these

groupname ,Member1,                                         Count
                    Member2
                    Member3
                    Nested group name
                                                   Member1
                                                   Member2
                                                   Member3
The count should not include any nested group but should include members within the nested group
If you add a string delimiter on line 16,

EX: delimiter: "|":  Add-Content C:\ee.csv -Value "$xGName`,`|$xGMembers`|`,$xGCount"}

Wen you run the "Text to columns" in excel, place the "|" char as the string delimiter and the "," as the column delimiter

Thanks but still confusing

Can you remove all CN=,OU=,DN= all those paths and have just names
ASKER CERTIFIED SOLUTION
Avatar of x-men
x-men
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I get this

You cannot call a method on a null-valued expression.
At line:3 char:34
+     $o.GName=$group.Name.ToString <<<< ();
    + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:4 char:53
+     $o.GMembers=$Group.member | foreach {$_.tostring <<<< ().substring(3,$_.tostring().IndexOf(",")-3)};
    + CategoryInfo          : InvalidOperation: (tostring:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
have you altered line 1 with the correct LDAP path ?
Are there any AD objects of the class "Group" in the LDAP path you provided?
Yes path is fine and it has the groups as well
well, the mesage

null-valued expression.
At line:3 char:34
+     $o.GName=$group.Name

tells me that there are no objects of class 'Group' in the LDAP Path provided at line 1. so you might want to double check it. The machine is always right you know...
Sorry for the delay will check and get back