Home > database >  How to indent command output in powershell
How to indent command output in powershell

Time:09-29

I am trying to present a ping command output in a nice indent... But, can't seem to format it properly....

I want an output like this :

Result: 
        Pinging google.com [216.58.208.110] with 32 bytes of data:
        Reply from 216.58.208.110: bytes=32 time=223ms TTL=61
        Reply from 216.58.208.110: bytes=32 time=129ms TTL=61

        Ping statistics for 216.58.208.110:
            Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
        Approximate round trip times in milli-seconds:
            Minimum = 129ms, Maximum = 223ms, Average = 176ms

However Powershell doesn't seem to want to allow me to do it without messing up the output

Here is the code block I am working with

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 | Out-String
Write-Host ""
Write-Host "$ping" -ForegroundColor Green
Write-Host ""

which sort of works, but with no indentation....

When I tried different variations trying to get the indent, well they all failed misserably....

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 | Out-String
Write-Host ""
Write-Host "      $ping" -ForegroundColor Green
Write-Host ""

Result: This sort of worked, but only the first line was indented... The rest of the output was not...

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 | Format-Table 
Write-Host ""
Write-Host "    $ping" -ForegroundColor Green
Write-Host ""

Result: Powershell spit out a windows error and the table never did appear....

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1
foreach ($line in $ping ) {
  Write-Host ""
  Write-Host "      $line" -ForegroundColor Green
  Write-Host ""
}

Result: the first 3 lines of the input were aligned left and the remaining lines indented as expected...

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 
$indent = $ping.PadLeft(50)
Write-Host ""
Write-Host "    $indent" -ForegroundColor Green
Write-Host ""

Result: It was like I hit the align right button in word and the output was messed up...

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1
foreach ($line in $ping ) {
    Write-Host ""
    Write-Host "`t $line" -ForegroundColor Green
    Write-Host ""
}

Result: The tab never did appear, and the output was left aligned with no indentation...

Can someone please point out the error of my ways... It's been driving me crazy for the last few days...

Thanks in advance!

x

CodePudding user response:

Most command line tools output one line at a time, so drop Out-String and just prefix each line with the required amount of whitespace:

$ping = & { ping -n 2 google.com } 2>&1
$ping |ForEach-Object {"    $_"}

If a tool outputs multi-line strings (or if you really like Out-String), you could use the -replace regex operator to add leading space at the start of the string and after any newline - this will work regardless of whether you have multiple single-line strings or one (or multiple) multi-line strings:

PS ~> $ping = & { ping -n 2 google.com } 2>&1 |Out-String
PS ~> $ping -replace '(?<=^|\n)',"`t"

        Pinging google.com [142.251.36.14] with 32 bytes of data:
        Reply from 142.251.36.14: bytes=32 time=12ms TTL=117
        Reply from 142.251.36.14: bytes=32 time=13ms TTL=117

        Ping statistics for 142.251.36.14:
            Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
        Approximate round trip times in milli-seconds:
            Minimum = 12ms, Maximum = 13ms, Average = 12ms

The regex pattern (?<=^|\n) matches any position immediately after the start of the string (^) or a literal newline character (\n). "`t" is the escape sequence for a literal TAB, so we're telling PowerShell to insert a tab at any such position in the input string.

  • Related