Formatting Text

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 1 month ago.

  • Author
    Posts
  • #14064
    Profile photo of Zeff Wheelock
    Zeff Wheelock
    Participant

    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!

  • #14065
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    #>
    
  • #14066
    Profile photo of Zeff Wheelock
    Zeff Wheelock
    Participant

    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.

  • #14067
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

You must be logged in to reply to this topic.