Author Posts

March 25, 2014 at 4:56 pm

I have been searching for an answer and I thought I saw the answer on one of the Microsoft Virtual Academy videos for Powershell. I want to try do a Get-ADUser showing several fields in a table format. The issue I am having is trying to format the ProxyAddresses attribute to list one address per line with the ProxyAddresses as the header. Ideally, I would like to see the following

Name ProxyAddresses
---------------------------- ---------------------------------
John Doe John_Doe@abc.com
John_Doe@def.com
John_Doe@ghi.com

The part I saw on MVA was Jason using pipes to eventually get this type of output. Unfortunately, I do not have the time to go through all 8 hrs of the videos again to find it. Thanks for the help!

March 25, 2014 at 5:06 pm

I don't have an AD environment up and running at the moment to test this, so I've simulated an object with a Name and ProxyAddresses property, and formatted it the way you mentioned by a combination of Select-Object (with a constructed property) and Format-Table -Wrap. (You can also throw in the -AutoSize parameter to Format-Table, if you prefer.)

$fakeUser = New-Object psobject -Property @{
    Name = 'John Doe'
    ProxyAddresses = @(
        'John_Doe@abc.com'
        'John_Doe@def.com'
        'John_Doe@ghi.com'
    )
}

Write-Host 'Default output:'
$fakeUser | Out-Host

Write-Host ''
Write-Host 'Multi-line formatted output:'

$selectProperties = @(
    'Name'
    @{
        Name = 'ProxyAddresses'
        Expression = { $_.ProxyAddresses -join "`r`n" }
    }
)

$fakeUser |
Select-Object -Property $selectProperties |
Format-Table -Wrap |
Out-Host

< #
Default output:

Name                                                                                                             ProxyAddresses                                                                                                 
----                                                                                                             --------------                                                                                                 
John Doe                                                                                                         {John_Doe@abc.com, John_Doe@def.com, John_Doe@ghi.com}                                                         



Multi-line formatted output:

Name                                                                                                             ProxyAddresses                                                                                                 
----                                                                                                             --------------                                                                                                 
John Doe                                                                                                         John_Doe@abc.com                                                                                               
                                                                                                                 John_Doe@def.com                                                                                               
                                                                                                                 John_Doe@ghi.com
#>

March 25, 2014 at 6:23 pm

That will work. I did the following:

$selectProperties = @(
    'Name'
    @{
        Name='ProxyAddresses'
        Expression={ $_.ProxyAddresses -join "`r`n" }
    }
)
get-aduser user -Properties Name,ProxyAddresses | Select-Object -Property $selectProperties | format-table -Wrap | Out-Host

Did Jason do something different when he was doing the MVA video that got it to do the same thing on the same line? I assume I can put everything in the @() block with semicolons replacing the variable. But I swear there was a single line he did this with.

March 25, 2014 at 6:30 pm

Beats me, I don't have those videos memorized. 🙂 I separate things onto multiple lines to make them more readable, but technically, there's nothing there that you couldn't jam into a single line. For example:

Get-ADUser -Properties Name,ProxyAddress | Select Name,@{n='ProxyAddresses';e={$_.ProxyAddresses -join "`r`n"}} | ft -Wrap

Personally, I find that kind of one-liner to be an eyesore, and I never put them into my scripts. I occasionally do that sort of thing at the console (interactively), but that's about it.