Skip to content

Instantly share code, notes, and snippets.

@KirkMunro
Last active July 19, 2018 07:25
Show Gist options
  • Save KirkMunro/efcae5b3b0d2bb94abf4 to your computer and use it in GitHub Desktop.
Save KirkMunro/efcae5b3b0d2bb94abf4 to your computer and use it in GitHub Desktop.
Show a custom sorted grid of all PowerShell verbs in an easy to read format
<#
.SYNOPSIS
Show all verbs in an easy-to-read grid.
.DESCRIPTION
Show all verbs in a grid, with custom sorting applied so that we can quickly scan each column and find verbs more easily.
This command was designed for ad-hoc use only. To get the most value out of this command, place it in your profile script. It outputs format data which is not easily used in automated commands.
#>
function verbs {
# Get the verbs in a sorted list
$verbs = Get-Verb | Sort-Object -Property Verb | Select-Object -ExpandProperty Verb
# Figure out the number of columns, the minimum number of verbs per column, and the
# number of columns containing an extra verb
$columnCount = [System.Math]::Min($Host.UI.RawUI.BufferSize.Width / 'ConvertFrom '.Length -as [System.Int32], 10)
$columnsWithOneMore = 0
$rowCount = [System.Math]::DivRem($verbs.Count, $columnCount, [REF]$columnsWithOneMore)
if ($columnsWithOneMore -gt 0) {
$rowCount++
}
# Add a buffer to the array to make the resulting wide grid a rectangle
$buffer = @('') * ($columnCount - $columnsWithOneMore)
$verbs += $buffer
# Once we have enough entries to produce a rectangular grid, flip the rows
# and the columns
$reorderedVerbs = New-Object -TypeName System.Collections.ArrayList
for ($rowIndex = 0; $rowIndex -lt $rowCount; $rowIndex++) {
for ($columnIndex = 0; $columnIndex -lt $columnCount; $columnIndex++) {
$reorderedVerbs.Add($verbs[$rowIndex + $columnIndex * $rowCount]) > $null
}
}
# With the grid reordered, we can output the results
$reorderedVerbs | Format-Wide -Property {$_} -Column $columnCount -Force
}
@wimjan
Copy link

wimjan commented Feb 24, 2016

Why not use: Format-Wide -AutoSize

@KirkMunro
Copy link
Author

Good question @wimjan. Format-Wide -AutoSize creates a wide grid that takes up less space, but the data is row-oriented rather than column oriented. For example, if you were showing values 1-7, with Format-Wide -AutoSize you get this:

1 2 3
4 5 6
7

With the function above, you get this instead:

1 4 7
2 5
3 6

You can see this with the Get-Verb function by comparing the results of this:

Get-Verb | Sort-Object -Property Verb | Format-Wide -AutoSize -Property Verb

with this:

verbs

Many people find the second example much easier to read/scan for information, because it matches how we actually read sorted lists of information. This is why I created this function, to make Format-Wide output (which is really only useful on sorted data anyway) more readable. I have an even better solution coming soon that addresses this issue for any Format-Wide output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment