How to Remove Blank Lines from PowerShell Format-Table Output?

As a PowerShell developer, you may have encountered situations where the output from the Format-Table cmdlet includes unwanted blank lines. These extra lines can make the output harder to read and consume more space than necessary. In this PowerShell tutorial, I’ll show you several methods to remove those pesky blank lines and make your PowerShell output cleaner without any blank lines.

For all the examples, I will use the .txt file below. You can run the below command to create the text file. You can see, this file contains some blank lines.

@"
United States of America

Capital: Washington, D.C.

Largest State: Alaska

National Bird: Bald Eagle

Currency: US Dollar (USD)


Motto: In God We Trust

"@ | Set-Content C:\Bijay\file.txt

Method 1: Using the Where-Object Cmdlet

One of the easiest ways to remove blank lines from your Format-Table output is by using the Where-Object cmdlet. This cmdlet allows you to filter the output based on certain conditions. Here’s how you can use it:

Get-Content .\file.txt | Where-Object {$_.trim() -ne "" } | Set-Content .\output.txt

In this example, Get-Content sends the lines into the Where-Object filter, which only returns non-blank lines for Set-Content to write to the output file. The trim() method removes any leading or trailing whitespace from each line, ensuring that lines containing only spaces are also considered blank.

You can also write the script like below:

$cleanedContent = Get-Content C:\Bijay\file.txt | Where-Object { $_.Trim() -ne "" } 
$cleanedContent  # Prints output in the console
$cleanedContent | Set-Content C:\Bijay\output.txt  # Saves to output.txt

or you can also write the cmdlet like below:

$cleanedContent = Get-Content C:\Bijay\file.txt | Where-Object { $_.Trim() -ne "" } 

# Display output in table format
$cleanedContent | ForEach-Object { [PSCustomObject]@{ Line = $_ } } | Format-Table -AutoSize

# Save cleaned content to output.txt
$cleanedContent | Set-Content C:\Bijay\output.txt

I executed the above script using VS code and you can see the exact output in the screenshot below:

Remove Blank Lines from PowerShell Format-Table Output

Check out Remove the First and Last Lines from a File Using PowerShell

Method 2: Piping to Out-String and Trim()

Another approach to removing blank lines is by combining the Out-String and Trim() methods in PowerShell. This method is particularly useful when you want to remove blank lines from the beginning and end of your output. Here’s an example:

Get-Process | Format-Table | Out-String | ForEach-Object { $_.Trim() }

In this case, the output from Get-Process is piped to Format-Table, then to Out-String to convert it to a string. Finally, the Trim() method is applied to each line using ForEach-Object, removing all blank lines from the output.

Method 3: Using Regular Expressions

You can use regular expressions to remove blank lines from your PowerShell output for more advanced scenarios. This method provides greater flexibility and control over the filtering process. Here’s an example:

$output = Get-Content .\file.txt | Out-String
$output -replace "(?m)^\s*`r`n", "" | Set-Content .\output.txt

In this example, the content of file.txt is read using Get-Content and converted to a string using Out-String. Then, a regular expression is used with the -replace operator to remove blank lines.

The regular expression (?m)^\s*\r\n matches any line that contains only whitespace characters followed by a carriage return and newline. The matched lines are replaced with an empty string, effectively removing them from the output.

You can also write the script like below to print in the output window.

$output = Get-Content C:\Bijay\file.txt | Out-String
$newoutput= $output -replace "(?m)^\s*`r`n", ""
$newoutput

You can see the exact output in the screenshot below:

How to Remove Blank Lines from PowerShell Format-Table Output

Conclusion

Removing blank lines from your PowerShell Format-Table output is a simple but effective way to improve readability and save space. In this tutorial, I explained how to remove blank lines from your PowerShell Format-Table output using different methods such as: using the Where-Object cmdlet, piping to Out-String and Trim(), or using regular expressions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.