-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFormat-ColumnTable.ps1
92 lines (73 loc) · 3.12 KB
/
Format-ColumnTable.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Function Format-ColumnTable {
<#
.SYNOPSIS
Formats the output as a table.
.DESCRIPTION
The Format-ColumnTable cmdlet formats the outout of a command as a table with the selected property shown in columns. It will only show one property.
.PARAMETER InputObject
Specifies the objects to format. Enter a variable that contains the objects, or type a command or expression that gets the objects.
.PARAMETER ColumnCount
Sepcifies the number of columns to wrap the output too. The default is as many will fit in the current windows width
.EXAMPLE
Get-Service | Format-ColumnTable
Returns the list of services, selects just the Name property and displays them.
.EXAMPLE
Format-ColumnTable -InputObject @(Get-Service | Select-Object -ExpandProperty Name) -ColumnCount 3
As above, but formats the output into three columns.
.NOTES
For additional information please see my GitHub wiki page
.LINK
https://github.com/My-Random-Thoughts
#>
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[string[]]$InputObject,
[int]$ColumnCount = -1,
[Parameter(DontShow)]
[switch]$Padded
)
Begin {
[string]$padding = ''
If ($Padded.IsPresent) { $padding = ' ' }
[int]$width = ($Host.UI.RawUI.BufferSize.Width)
[int]$rowCnt = 0
[int]$colCnt = 0
[string[]]$allObjects = @()
[string] $lineOutput = $padding
}
Process {
If ($MyInvocation.ExpectingInput) { # Pipeline input, add each object to $allObjects
$inputItem = $_
Switch ($true) {
{ $inputItem -is [string] } { $allObjects += $inputItem ; Break }
{ $inputItem.Name -is [string] } { $allObjects += $inputItem.Name; Break }
Default { $allObjects += $inputItem ; Break }
}
}
Else {
$allObjects = $InputObject # Non-Pipeline input
}
}
End {
$allObjects = ($allObjects | Sort-Object)
[int]$length = (($allObjects | Measure-Object -Maximum -Property Length).Maximum)
[int]$numOfCols = ($width / ($length + 5 + ($padding.Length)))
If ($ColumnCount -gt 0) { $numOfCols = $ColumnCount }
[int]$numOfRows = [math]::Ceiling(($allObjects.Count) / $numOfCols)
If ($Padded.IsPresent) { Write-Output '' }
Do {
If ($colCnt -eq $numOfCols) {
Write-Output $lineOutput
$lineOutput = $padding
$rowCnt++
$colCnt = 0
}
[int]$index = (($colCnt * $numOfRows) + $rowCnt)
If (($index -lt $allObjects.Count) -and ($rowCnt -lt $numOfRows)) {
$lineOutput += "$(($allObjects[$index]).PadRight($length)) "
}
$colCnt++
} Until (($rowCnt -eq $numOfRows) -and ($colCnt -eq $numOfCols))
If ($Padded.IsPresent) { Write-Output '' }
}
}