Sunday, October 2, 2022
HomePowershellOpen Up Vast • The Lonely Administrator

Open Up Vast • The Lonely Administrator


I just lately learn a terrific submit from Mike F. Robbins about utilizing Format-Vast to show strings. Format-Vast is a type of cmdlets that I don’t suppose will get a lot use. This can be a disgrace as a result of it definitely fulfills a necessity. However as Mike factors out, when you’ve got an inventory of strings, Format-Vast requires somewhat further work, which Mike demonstrates. I made a decision to take his thought and run with it. Wouldn’t or not it’s simpler to have a perform?

Handle and Report Energetic Listing, Alternate and Microsoft 365 with
ManageEngine ADManager Plus – Obtain Free Trial

Right here’s a PowerShell perform based mostly on Mike’s notes.

Perform 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
    )
    Start {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Beginning $($myinvocation.mycommand)"

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

        #outline a hashtable to splat to Format-Vast on the finish
        $fw = @{
            Pressure    = $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)
        }
    } #start

    Course of {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Including $InputObject"
        $strings.add($InputObject)
    } #course of

    Finish  Format-Vast @fw
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
     #finish

} #shut Out-WideString

The perform assumes you’ll pipe a sequence of strings. The perform parameters mirror these of Format-Vast.

The default conduct is to autosize the output.

Out-Widestring autosize default

Or you may specify the variety of columns.

out-widestring columns

As with the opposite Out cmdlets, you may’t do something with the output of Out-WideString aside from ship it to a file or printer.

Get-Content material C:scriptswomen.txt | Out-WideString | out-file c:worknames.txt

Thanks, Mike, for the tip. In case you have any questions on how my perform works, be at liberty to depart a remark.


Learn PowerShell Scripting in a Month of Lunches, 2nd Ed. MEAP


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments