Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Open Up Wide

Posted on May 26, 2022May 26, 2022

I recently read a terrific post from Mike F. Robbins about using Format-Wide to display strings. Format-Wide is one of those cmdlets that I don't think gets much use. This is a shame because it certainly fulfills a need. But as Mike points out, if you have a list of strings, Format-Wide requires a little extra work, which Mike demonstrates. I decided to take his idea and run with it. Wouldn't it be easier to have a function?

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Here's a PowerShell function based on Mike's notes.

Function Out-WideString {
    [cmdletbinding(DefaultParameterSetName = "autosize")]
    [alias("ows")]
    Param(
        [Parameter(ValueFromPipeline, HelpMessage = "The strings to display in a wide format.")]
        [string]$InputObject,
        [Parameter(ParameterSetName = "columns", HelpMessage = "The number of columns to display the strings in. The default is 3")]
        [int]$Columns = 3,
        [Parameter(ParameterSetName = "autosize", HelpMessage = "Autosize the output.")]
        [switch]$AutoSize
    )
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"

        #initalize a collection to hold the incoming strings
        $strings = [System.Collections.Generic.list[string]]::new()

        #define a hashtable to splat to Format-Wide at the end
        $fw = @{
            Force    = $true
            Property = { $_ }
        }
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Detected parameter set $($PSCmdlet.ParameterSetName)"
        if ($pscmdlet.ParameterSetName -eq 'AutoSize') {
            Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Autosizing output"
            $fw.Add("Autosize", $true)
        }
        else {
            Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Setting column width to $Columns"
            $fw.Add("Column", $Columns)
        }
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Adding $InputObject"
        $strings.add($InputObject)
    } #process

    End {
        #format the strings wide
        $strings | Format-Wide @fw
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end

} #close Out-WideString

The function assumes you are going to pipe a series of strings. The function parameters mirror those of Format-Wide.

Out-WideString help

The default behavior is to autosize the output.

Out-Widestring autosize default

Or you can specify the number of columns.

out-widestring columns

As with the other Out cmdlets, you can't do anything with the output of Out-WideString other than send it to a file or printer.

Get-Content C:\scripts\women.txt | Out-WideString | out-file c:\work\names.txt

Thanks, Mike, for the tip. If you have any questions on how my function works, feel free to leave a comment.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Open Up Wide”

  1. Pingback: Open Up Wide - The Lonely Administrator - Syndicated Blogs - IDERA Community

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d