Converting Fixed-Width Text Data

by May 11, 2016

PowerShell can turn fixed-width tabular data easily into rich objects.

First, define the column widths. Then, have PowerShell inject a delimiter at these positions. Finally, ConvertFrom-Csv can split the text lines and turn the text into rich objects:

# get fixed-column width text content, 
# for example, take the result from qprocess.exe
$content = qprocess.exe

# define column breaks in descending order
$columns = 23, 43, 49, 54 | Sort-Object -Descending
$newDelimiter = ','

# inject commas into each line
$content | ForEach-Object {
  $line = $_
  $columns | ForEach-Object {
    $line = $line.Insert($_, $newDelimiter)  
  }
  $line
} |
ConvertFrom-Csv -Delimiter $newDelimiter

Twitter This Tip! ReTweet this Tip!