powershelladgroup

Powershell script to display all Users in a Group AD


I have created the below

Get-ADGroup -Filter * -SearchBase "DC=Domain,dc=.com" -properties name, members | 
    Select-Object *,@{Name='Member';Expression={$_.Members -replace '^CN=([^,]+).+$','$1'}} | 
    FT Name, Member -Autosize | 
    out-file c:\text.txt

Ignore Domain and .com I have them populated with my relevant information, but for sake of here removed them.

When I run this it returns what I'm after but when looking at the members within the group they all end with ... and don't show all the members


Solution

  • There are a few things to correct. Let's look at them in order. The actual AD query can be simplified: you only need to specify 'Members' as an additional property to retrieve as 'Name' is brought back by default:

    Get-ADGroup -Filter * -SearchBase "DC=Domain,dc=.com" -properties members
    

    Given that you only want to output two properties ('Name' and your custom one 'Member'), use your select to retrieve only the ones you want:

    Select-Object Name ,@{Name='Member';Expression={$_.Members -replace '^CN=([^,]+).+$','$1'}}
    

    Remove the Format-Table: we have already limited the selection in the previous command. Format cmdlets are designed to format the output to the console window and best practice dictates that they should only be used for that purpose and that they should always be the last element of a pipeline.

    Piping all of that to Export-Csv will then produce what you want:

    Export-Csv -NoTypeInformation -Path C:\text.csv