Search code examples
powershellcsvactive-directory-group

Export Multiple Groups to CSV


I have a little script that exports a certain group to a csv file.

The question is if it is possible to insert a split in the group name to export twp groups into two separate csv files.

param (
 [Parameter(Mandatory, ValueFromPipelineByPropertyname)]
 [ValidateNotNullOrEmpty()]
 [string]$GroupName
)

Get-ADGroupMember -identity “$GroupName” | select-object SamAccountName | Export-csv -path C:\Report\$GroupName.csv -NoTypeInformation 

Is it possible to write " Group1 Group2 " and the export to two files, Group1.csv and Group2.csv?


Solution

  • Try this:

    param (
     [Parameter(Mandatory, ValueFromPipelineByPropertyname)]
     [ValidateNotNullOrEmpty()]
     [string]$GroupName
    )
    $allGroupNames = $GroupName -split " "
    foreach( $GN in $allGroupNames ) {
        Get-ADGroupMember -identity “$GN” | select-object SamAccountName | Export-csv -path C:\Report\$GN.csv -NoTypeInformation 
    }