user3667364
user3667364

Reputation: 3

Get-ADGroupMember from list

Attempting to run 1 ps script by having the groups in a text file..

    $groups = Get-Content c:\groups.txt {
    foreach($Group in $Groups) 

    Get-ADGroupMember -identity "$groups" | select samaccountname,name "c:\test.txt" }

Missing statement body in foreach loop. At line:3 char:1 + <<<< Get-ADGroupMember -identity "$groups" | select samaccountname,name >> "c:\test.txt" } + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingForeachStatement

Upvotes: 0

Views: 3073

Answers (2)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

You will need to split your input file up with the newline character. You can pipe all groups into a single 'Select' to output too:

(Get-Content c:\groups.txt) -split '\n' | % {
    Get-ADGroupMember -identity $_
} | select samaccountname,name | Out-File "c:\test.txt"

Edit: This will use a calculated property so you can capture the group name:

(Get-Content c:\groups.txt) -split '\n' | % {
    Get-ADGroupMember -identity $_ | select @{n="Group";e={$_}},samaccountname,name
} | Out-File "c:\test.txt"

If you end up converting the output to a spreadsheet, you may consider ConvertTo-Csv also.

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72176

Since I'm not sure what you are trying to achieve, I've modified this to remove all the obvious errors.

Get-Content c:\groups.txt -OutVariable groups | foreach {
    Get-ADGroupMember -identity $_ | select samaccountname,name | 
        out-file c:\test.txt -Append }

Upvotes: 0

Related Questions