List of Members per Group to CSV

Hi All,

How do you export this to a csv file.

Get-PWGroup | %{ Write-Host $_.GroupName:; $_ } | Get-PWGroupMember

I've tried a few different way's and can only get the list to come out not the group headings

$groupmemberspath = 'C:\temp\Export PWGroupMembers.csv'
Get-PWGroup | %{ Write-Host $_.GroupName:; $_ } | Get-PWGroupMember | Export-CSV -path $groupmemberspath

End game is to get a CSV with a full list of groups with users in each group.

Thanks
Ray

Parents
  • Ray,

    Get-PWGroup and Get-PWGroupMember are found in PWPS, and would rather not try to make that work as there is an alternative way to do what I think you want to accomplish if you use PWPS_DAB.

    Here's a sample script that will create an XLSX file with each group as a worksheet, and each worksheet containing some user information.

    New-PWLogin
    
    $GroupNames = Get-PWGroupNames
    $dta = New-Object System.Collections.Generic.List[System.Object]
    foreach ($G in $GroupNames)
    {
        $dt = Get-PWMembers -Name $G -Type Group | ConvertTo-DataTable
        $dt.TableName = $G
        $dta.Add($dt)
    }
    
    $dta | New-XLSXWorkbook -OutputFileName 'c:\temp\GroupsByDataArray.xlsx'
    
    Undo-PWLogin
    

    However, this may not work for all of your groups.  In my testing, if the group's name is not valid for an XLSX worksheet, then that will fail. 

    You could rename your groups to something that Excel will consider to be valid, or in your script, inspect the group's name, and if it would be invalid in Excel, then then rename it something acceptable to Excel.  You could also skip any empty groups by tweaking the sample script.

    Dan Williams
    Solution Consultant
    Bentley Systems, Incorporated
    Portland, OR, USA (Pacific Time UTC-08:00)

    Answer Verified By: Ray Lucas 

Reply Children
No Data