Tutorial :How to remove extra space between columns in Powershell Format-Table



Question:

I'm converting some csv data to a file format that expects values a certain positions in a line. I thought I'd use format-table to accomplish this by setting the column widths. However, this adds an unwanted extra space between columns. Is there a way to remove the space or are there other formatting options that I could use?

$format =   @{E={$_.lname}; width=30},  @{E={$_.fname}; width=30},  @{E={$_.dob}; width=8},  @{E={if($_.sex -eq 'F') { 'V' } elseif($_.sex -ne 'M') { 'O' } else { $_.sex} }; width=1},    ...    @{E={if($_.mstatus -eq 'M') {'Y'} else {'N'}}; width=1;},  @{E={$_.social}; width=50};    import-csv -Delimiter '|' .\data.txt | ft $format -hidetableheaders | out-file -width 2500 'c:\temp\temp.txt'  


Solution:1

There's not a way to get rid of the extra space output by Format-Table AFAIK. Try outputting as a string e.g.:

... | Foreach { '{0,-30}{1,-30}{2,-8}' -f $_.lname, $_.fname, $_.dob }  

Note that this is a .NET formatting string and all the normal .NET formatting directives apply.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »