Is there any Powershell module similar to Python prettytable?
Question:
In python we have prettytable module for easily displaying tabular data in a visually appealing ASCII table format.
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
Do we have anything similar module in PowerShell?
Answers:
Not quite the same but maybe Format-Table
Edit:
I threw the data you had in your question into a CSV to test it, works about the same I think.
The canonical answer is of course Format-Table
, as mentioned by others.
Where Ajay says "it gets scrambled" is due to a misapprehension of the output of Format-Table
and Format-List
. These commands output internal markup. That markup is rendered by the engine to stdout. It is not rendered if you pipe the output of a Format
command to another command (say, ConvertTo-Html
). In that case, the next command receives the markup instead of the raw data, which is almost never what you want.
The markup is also rendered by Out-String
. So do this:
> $AsciiTable = Get-Process pwsh | Format-Table | Out-String
Output:
> $AsciiTable
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
115 143.79 207.55 9.55 1080 1 pwsh
92 129.62 188.68 10.77 2340 1 pwsh
89 125.49 183.95 11.31 11352 1 pwsh
To get a [string[]]
instead of a multi-line [string]
, use Out-String -Stream
.
To trim the empty lines at the head and foot, use .Trim()
:
PSv5+: $AsciiTable = Get-Process pwsh | Format-Table | Out-String | ForEach-Object Trim
Older PS: $AsciiTable = Get-Process pwsh | Format-Table | Out-String | ForEach-Object {$_.Trim()}
Alternatives
ConvertTo-Markdown
Ships with PSv7, present on 7.2.6, YMMV on older versions
> Get-ChildItem | Select-Object Mode, LastWriteTime, Length, Name | ConvertTo-Markdown
Mode | Name | LastWriteTime
----- | --------------- | -------------------
d---- | dotfiles | 05/10/2022 11:35:34
d---- | Notes | 05/10/2022 11:29:09
d---- | oneget | 06/10/2022 18:50:00
If I specifically wanted a close match to the prettytable
output and could rely on recent PS, I’d use this, then add the border myself.
ConvertTo-Html
Produces HTML tables. Depends on your intended use.
Community modules
Pick a published module (I have not examined these, I’m demonstrating use of Find-Module -Tag
):
> Find-Module -Repository PSGallery -Tag ascii
Version Name Repository Description
------- ---- ---------- -----------
1.2.2.1 WriteAscii PSGallery Use Svendsen Tech's Write-Ascii function…
1.0.2 Graphical PSGallery Consumes data points as input and plots …
0.0.0.1 Encoding PSGallery Cmdlets for determining file encodings
1.0.3 EncodingAnalyzer PSGallery determines the encoding of any text file
0.1.0.0 SystemSplash PSGallery An ascii system information display for …
0.1.2 SimplyDraw PSGallery ASCII Graphic tooling for PowerShell
1.0.2 Image2Text PSGallery PowerShell Images into ASCII art
In python we have prettytable module for easily displaying tabular data in a visually appealing ASCII table format.
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
Do we have anything similar module in PowerShell?
Not quite the same but maybe Format-Table
Edit:
I threw the data you had in your question into a CSV to test it, works about the same I think.
The canonical answer is of course Format-Table
, as mentioned by others.
Where Ajay says "it gets scrambled" is due to a misapprehension of the output of Format-Table
and Format-List
. These commands output internal markup. That markup is rendered by the engine to stdout. It is not rendered if you pipe the output of a Format
command to another command (say, ConvertTo-Html
). In that case, the next command receives the markup instead of the raw data, which is almost never what you want.
The markup is also rendered by Out-String
. So do this:
> $AsciiTable = Get-Process pwsh | Format-Table | Out-String
Output:
> $AsciiTable
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
115 143.79 207.55 9.55 1080 1 pwsh
92 129.62 188.68 10.77 2340 1 pwsh
89 125.49 183.95 11.31 11352 1 pwsh
To get a [string[]]
instead of a multi-line [string]
, use Out-String -Stream
.
To trim the empty lines at the head and foot, use .Trim()
:
PSv5+: $AsciiTable = Get-Process pwsh | Format-Table | Out-String | ForEach-Object Trim
Older PS: $AsciiTable = Get-Process pwsh | Format-Table | Out-String | ForEach-Object {$_.Trim()}
Alternatives
ConvertTo-Markdown
Ships with PSv7, present on 7.2.6, YMMV on older versions
> Get-ChildItem | Select-Object Mode, LastWriteTime, Length, Name | ConvertTo-Markdown
Mode | Name | LastWriteTime
----- | --------------- | -------------------
d---- | dotfiles | 05/10/2022 11:35:34
d---- | Notes | 05/10/2022 11:29:09
d---- | oneget | 06/10/2022 18:50:00
If I specifically wanted a close match to the prettytable
output and could rely on recent PS, I’d use this, then add the border myself.
ConvertTo-Html
Produces HTML tables. Depends on your intended use.
Community modules
Pick a published module (I have not examined these, I’m demonstrating use of Find-Module -Tag
):
> Find-Module -Repository PSGallery -Tag ascii
Version Name Repository Description
------- ---- ---------- -----------
1.2.2.1 WriteAscii PSGallery Use Svendsen Tech's Write-Ascii function…
1.0.2 Graphical PSGallery Consumes data points as input and plots …
0.0.0.1 Encoding PSGallery Cmdlets for determining file encodings
1.0.3 EncodingAnalyzer PSGallery determines the encoding of any text file
0.1.0.0 SystemSplash PSGallery An ascii system information display for …
0.1.2 SimplyDraw PSGallery ASCII Graphic tooling for PowerShell
1.0.2 Image2Text PSGallery PowerShell Images into ASCII art