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?
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.

The default behavior is to autosize the output.

Or you can specify the number of 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.
1 thought on “Open Up Wide”
Comments are closed.