0
votes

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

2

2 Answers

1
votes

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.

0
votes

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 }