We’ve lately launched one other preview of the TextUtility module. This module is a set of instruments that
are supposed to assist with working with textual content content material.
Putting in the module
You possibly can set up this module from the PowerShellGallery with PowerShellGet through:
Set up-Module -Title Microsoft.PowerShell.TextUtility -AllowPrerelease
Or you may set up it with PSResourceGet through:
Set up-PSResource -Title Microsoft.PowerShell.TextUtility -Prerelease
ConvertFrom-TextTable
The latest pre-release of Microsoft.PowerShell.TextUtility
has some new thrilling performance.
The brand new ConvertFrom-TextTable
cmdlet means that you can take tabular textual content
and convert it into objects.
Additionally, there’s a technique to convert a few of varieties of these objects into one thing aside from a string.
Utilizing the -ConvertPropertyValue
parameter will change the worth of the property to a strongly typed worth.
This implies you are able to do the next:
PS> df | choose -first 6 | convertfrom-texttable -ConvertPropertyValue |Ft
Filesystem 512-blocks Used Accessible Capability iused ifree %iused Mounted_on
---------- ---------- ---- --------- -------- ----- ----- ------ ----------
/dev/disk4s1s1 3907805752 17699080 1242895000 2% 355382 4291123926 0% /
devfs 462 462 0 100% 800 0 100% /dev
/dev/disk4s3 3907805752 3676600 1242895000 1% 3745 6214475000 0% /System/Volumes/Preboot
/dev/disk4s5 3907805752 4194376 1242895000 1% 2 6214475000 0% /System/Volumes/VM
/dev/disk4s6 3907805752 19992 1242895000 1% 19 6214475000 0% /System/Volumes/Replace
PS> df | choose -first 6 | convertfrom-texttable -ConvertPropertyValue |?{$_.accessible -lt 10}|Ft
Filesystem 512-blocks Used Accessible Capability iused ifree %iused Mounted_on
---------- ---------- ---- --------- -------- ----- ----- ------ ----------
devfs 462 462 0 100% 800 0 100% /dev
ConvertFrom-TextTable
additionally means that you can specify the place the header line is outlined, or skip strains till you get to the place the info begins.
Moreover, it’s doable to take the textual content output and explicitly create properties by defining the place a column begins.
PS> $string = $("a".."z";"A".."Z";0..9) -join ""
PS> 1..10 | %{$string}|convertfrom-texttable -noheader -columnoffset 0,15,23,40,55 | ft
Property_01 Property_02 Property_03 Property_04 Property_05
----------- ----------- ----------- ----------- -----------
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
abcdefghijklmno pqrstuvw xyzABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789
Lastly, you too can emit the info as JSON with the -AsJson
parameter:
PS> who -p | %{$_.trim()} | convertfrom-texttable -noheader -AsJson
{ "Property_01": "james", "Property_02": "console", "Property_03": "Jun", "Property_04": "22", "Property_05": "08:34" }
{ "Property_01": "james", "Property_02": "ttys016", "Property_03": "Jun", "Property_04": "22", "Property_05": "10:00" }
{ "Property_01": "james", "Property_02": "ttys017", "Property_03": "Jun", "Property_04": "22", "Property_05": "14:42" }
{ "Property_01": "james", "Property_02": "ttys004", "Property_03": "Jun", "Property_04": "22", "Property_05": "22:08" }
{ "Property_01": "james", "Property_02": "ttys025", "Property_03": "Jun", "Property_04": "23", "Property_05": "00:38" }
Subsequent Steps
That is nonetheless in pre-release and we all know that there’s nonetheless work to be performed.
For instance, the parser will create an object out of the header/knowledge separator.
This can be a little trickier than at first it could appear.
The textual content is inspected and columns width is set based mostly on the inspected textual content.
These header/knowledge separator strains assist the parser perceive the place the columns are separated.
This implies we are able to’t simply toss them out because the textual content is being learn.
I’m certain you’ll discover extra issues that we are able to do,
and we’d like to get your suggestions on this new cmdlet so please give this module a attempt.
Please submit your suggestions to the repo instantly through the problems tab, right here.
Thanks a lot!
Jim Truher
PowerShell Staff