0

I am looking for a way to change the current output of the following command so it's easier to read.

$list= get-content "c:\userlist.txt"
Foreach ($user in $list)
{
    Write-Host $user
    Get-ADPrincipalGroupMembership $user |select name |Where-Object {$_.name -like "*admin*"}
    write-host " "
}

Current output is as follows:

user1

name                                                                                                                                                                                                                                
----                                                                                                                                                                                                                                
admin1
admin2                                                                                                                                                                                                                   

user2
admin3
admin4

I would like it to be displayed like the below or something similar without the name title and extra line breaks. Thanks!

User1
admin1
admin2

User2
admin3
admin4
0

1 Answer 1

1

I think this will work for you:

$list= get-content "c:\userlist.txt"
foreach ($user in $list)
{
    Write-Host $user
    Write-Host (Get-ADPrincipalGroupMembership $user | Where-Object {$_.name -like "*admin*"} | Select-Object -ExpandProperty name | Out-String) 
    Write-Host " "
}

I put Write-Host in there to make it consistent with the rest of your output. You have to use -ExpandProperty rather than -Property (the default) to get rid of the column header. Out-String makes the output go to the screen as-is, rather than being interpreted as an object and converted to a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.