Exporting ADGroupMember results to CSV

Hi,

I am currenlty running the following script to get a list of all members within a list of AD Security Groups

$Groups = Get-ADGroup -Filter {name -like "FIL_*"}

Foreach ($Group in $Groups)
{
Write-output $Group.name
Get-ADGroup -Identity $Group | Get-ADGroupmember | format-list name

}

I want to export the results to excel with the Security Group names as the headers and then going down each column would be the members of the group.

Everything i try either leaves the output to excel as a blank worksheet or has non meaning full information in the worksheet.

Could you please help me with this?


  • Edited by Caleb Deane Wednesday, July 15, 2015 3:47 AM
July 15th, 2015 3:20am

Get-ADGroup -Filter {name -like "FIL_*"} |
    Foreach-Object{
        $groupname=$_.name
        $_ | Get-ADGroupmember | 
           Select @{N='GroupName';E={$groupname}}, *
    } |
    Export-Csv groups.csv

What yo are asking cannot be done directly.  If youwant a custom Excel output you will have to learn how to write scritps that automate Excel as no one will write it for you.

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 5:37am

use "export-csv -append"

Won't work.
July 15th, 2015 5:38am

Try this (remove info u dont need)

$Groups = Get-ADGroup -Filter {name -like "FIL_*"}
$myCol = @()
ForEach ($Group in $Groups)
{
	$Members = @(Get-ADGroupMember "$Group")
	If($Members.count eq 0)
     {
	$MyObject = New-Object PSObject -Property @{
	 Group = $Group.name
         OU = ($Group.DistinguishedName -replace '^.+?,(CN|OU.+),$1')
         ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
         Member = "This group is empty"
	 Enabled = ""
	    }
	 $mycol += $MyObject
     }
	Else
     {
	    ForEach ($Member in $Members)
         {
		  try
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OU = ($Group.DistinguishedName -replace '^.+?,(CN|OU.+),$1')
		    ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
		    Member = $member.name
		    Enabled = (get-aduser $member.name).Enabled
		    }
		    $mycol += $MyObject
           }
		catch
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OU = ($Group.DistinguishedName -replace '^.+?,(CN|OU.+),$1')
		    ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
		    Member = $member.name
		    Enabled = ""
		    }
		    $mycol += $MyObject
           }
		}
	 }
}
$myCol |select Group,OU,ManagedBy,Member,Enabled #|export-Csv groups.csv -Delimiter ";" -Encoding UTF8 -NoTypeInformation

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 8:24am

Try this (remove info u dont need)

$Groups = Get-ADGroup -Filter {name -like "FIL_*"}
$myCol = @()
ForEach ($Group in $Groups)
{
	$Members = @(Get-ADGroupMember "$Group")
	If($Members.count eq 0)
     {
	$MyObject = New-Object PSObject -Property @{
	 Group = $Group.name
         OU = ($Group.DistinguishedName -replace '^.+?,(CN|OU.+),$1')
         ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
         Member = "This group is empty"
	 Enabled = ""
	    }
	 $mycol += $MyObject
     }
	Else
     {
	    ForEach ($Member in $Members)
         {
		  try
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OU = ($Group.DistinguishedName -replace '^.+?,(CN|OU.+),$1')
		    ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
		    Member = $member.name
		    Enabled = (get-aduser $member.name).Enabled
		    }
		    $mycol += $MyObject
           }
		catch
           {
		    $MyObject = New-Object PSObject -Property @{
		    Group = $Group.name
		    OU = ($Group.DistinguishedName -replace '^.+?,(CN|OU.+),$1')
		    ManagedBy = ($Group.ManagedBy -split ",*..=")[1]
		    Member = $member.name
		    Enabled = ""
		    }
		    $mycol += $MyObject
           }
		}
	 }
}
$myCol |select Group,OU,ManagedBy,Member,Enabled #|export-Csv groups.csv -Delimiter ";" -Encoding UTF8 -NoTypeInformation

July 15th, 2015 12:22pm

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

Other recent topics Other recent topics