All Articles

Get list of users from CSV with list of DL's

Header in the csv file should be DL

$PathToCsv = "groups.csv"

Import-Csv $PathToCsv |
    ForEach-Object {

        $group = $null

        $group = $_.DL
        Get-ADGroupMember $group |
            Select-Object samaccountname |
            ForEach-Object {

                New-Object psobject -Property @{
                    DL = $group
                    UserName = $_.samaccountname
                }
            }
    } |
    Export-Csv -NoTypeInformation "output.csv"