HELP with Foreach Loop

*I’m trying to create a simple PS script to list the members of various groups and format the output so it’s listed as Group Name and the members directly below it. With the script below, it’s listing the group names one after another and the groups all the servers together so I can’t identify which servers are members of which group. Any help would be appreciated.

$grouplist = @(get-content "D:\scripts\input\groups.txt")
foreach ($group in $grouplist)
{
Write-host $($group)
Get-ADGroupMember -Identity $group | Select Name
} 
*

You just want to see them on the screen? If so, add | Out-Host after your select statement. The *-Host commands go directly to the console where your other command powershell will collect so many items before outputting. It’s trying to be helpful. Piping to out-host will force it to write it to the console immediately and you’ll see it output in the order you expect. If you plan on using the data down the line, I’d look at removing the write-host and outputting objects with the info wanted.

Thank you for that suggestion to use Out-Host. That does exactly what I wanted it to do. I tried to pipe the results to a .txt file but that didn’t seem to work. Is there a different parm aside from Out-Host that directs output to a .txt file?

If you want to output something to a file you can use Out-File.

Other cmdlets able to write content to files are

and

Or … if you want to save structured data you could use

Thank you for that! I added the Out-File and it added the group membership to the .txt file but the group names did not. The group names went to host. How do I get the group names and membership all formatted in a .txt or .csv?

Did you even read my response? I said if you want to use the info down the line (file, other commands, etc), remove write-host. I specifically said the *-Host cmdlets write to the console. Hence the question “You just want to see them on the screen?”

1 Like

Hi - I did see your response and I must not understand what you meant. I removed the write-host and now I don’t get the group names on the console or in the output. Sorry for being dense, but I’ve tried moving things around to no avail. My latest

$grouplist = @(get-content “D:\input\groups.txt”)
foreach ($group in $grouplist)
{
$group
Get-ADGroupMember -Identity $group | Select Name| Out-File “d:\output.txt”
}

You want the text file to have the group name on a line, followed by lines of members, followed by the next group, and so on? What’s the intended use for this file? I’d recommend using structured output such as a CSV so you could simply reimport the information. The problem with your last attempt is you are putting the group out to the screen and the members to the file. Remove the out-file from inside the loop. Capture the output of the loop to a variable. Then output the variable to the file.

$newvariable = foreach($group in $grouplist){…}

$newvariable | Set-Content -Path \some\pathto\afile.txt

I appreciate you help with this. The purpose of going through this exercise is to automate a process of mapping out servers and who has access to them for audits. We have many servers that are members of many groups and many user groups so I’m trying to put something together where I can dump a server group list or user group list in a .txt file and run a script to generate the output.

The output result looks like this now. I’m not sure why it displaying the @(Name= before the server names.

Group Name
@{Name=server name}

$grouplist = @(get-content “D:\input\groups.txt”)
$loop = foreach ($group in $grouplist)
{
$group
Get-ADGroupMember -Identity $group | Select Name
$loop |Set-Content -Path “d:\output.txt”
}

You’re still trying to write the output inside the loop.


$grouplist = get-content D:\input\groups.txt

$loop = foreach ($group in $grouplist){
    $group
    Get-ADGroupMember -Identity $group |
        Select-Object -ExpandProperty Name
}

$loop | Set-Content -Path d:\output.txt
2 Likes

I can’t thank you enough Doug for your time and patience. That’s exactly what I was looking for in the output. I really appreciate it!

1 Like