3

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

Matt
  • 45,022
  • 8
  • 78
  • 119
Phil Skinner
  • 45
  • 1
  • 5
  • 1
    Pipe into a csv instead. It's format-table that is trucating the output inorder to display it. `Select-Object props | Export-CSV -NoType c:\text.txt` – Matt Dec 11 '15 at 13:26
  • That export-csv garbles all the text file :( "9e210fe47d09416682b841769c78b8a3",,,,, "27c87ef9bbda4f709f6b4002fa4af63c",,,,, "27c87ef9bbda4f709f6b4002fa4af63c",,,,, – Phil Skinner Dec 11 '15 at 13:30
  • Replace `FT Name, Member -Autosize` with `Out-String` ? – sodawillow Dec 11 '15 at 13:36

2 Answers2

2

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
ConanW
  • 486
  • 3
  • 7
  • Thank you very much this is exactly what I was after, still trying to get my head around powershell as only just started to look into it. On a side note is there a way to expand on the -filter to only include part of a group name? – Phil Skinner Dec 11 '15 at 14:08
  • The filter parameter will make your AD queries faster as using it will limit the scope of that query. In short it's something like this: `Get-ADGroup -Filter 'Name -like "SomeGroupNamePrefix*"'`. I'd advise looking at the help for the cmdlet: `Get-Help Get-ADGroup -Full` – ConanW Dec 11 '15 at 14:43
  • I'd be grateful for the vote if you have found this uesful – ConanW Dec 22 '15 at 11:34
1

This one did the trick for me

Get-ADGroupMember -Identity Administrators | Select-Object name, objectClass,distinguishedName | Export-CSV -Path “adgroupmembers.csv”

I got this here.

https://www.lepide.com/how-to/export-members-of-a-particular-ad-group-using-poweshell.html#:~:text=The%20PowerShell%20Get%2DADGroupMember%20cmdlet,group%20you%20want%20to%20use.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Rick
  • 11
  • 1