kahoots
kahoots

Reputation: 191

Changing Powershell Output

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

Upvotes: 0

Views: 38

Answers (1)

Brandon Stiff
Brandon Stiff

Reputation: 161

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.

Upvotes: 1

Related Questions