0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Remove Blank Lines from File

Use Get-Content input.txt | Where-Object { $_.Trim() -ne '' } | Set-Content output.txt to remove blank lines from a file in PowerShell.
📋

Examples

InputLine 1 Line 3 Line 6
OutputLine 1 Line 3 Line 6
Input
Output
InputFirst line Second line Third line
OutputFirst line Second line Third line
🧠

How to Think About It

To remove blank lines, read the file line by line, check if each line is not empty or only spaces using Trim(), then keep only those lines and write them back to a file.
📐

Algorithm

1
Read all lines from the input file.
2
For each line, remove spaces from start and end using <code>Trim()</code>.
3
Check if the trimmed line is not empty.
4
Keep lines that are not empty.
5
Write the filtered lines to the output file.
💻

Code

powershell
Get-Content input.txt | Where-Object { $_.Trim() -ne '' } | Set-Content output.txt
Write-Output "Blank lines removed and saved to output.txt"
Output
Blank lines removed and saved to output.txt
🔍

Dry Run

Let's trace the input file with lines: 'Line 1', '', 'Line 3', '', '', 'Line 6' through the code.

1

Read lines

["Line 1", "", "Line 3", "", "", "Line 6"]

2

Filter non-blank lines

Keep lines where Trim() is not empty: ["Line 1", "Line 3", "Line 6"]

3

Write output

Write filtered lines to output.txt

Original LineTrimmed LineKeep?
Line 1Line 1Yes
No
Line 3Line 3Yes
No
No
Line 6Line 6Yes
💡

Why This Works

Step 1: Read file lines

The Get-Content command reads the file line by line into an array.

Step 2: Filter blank lines

The Where-Object filters lines where Trim() removes spaces and checks if the line is not empty.

Step 3: Save filtered lines

The filtered lines are saved back to a file using Set-Content.

🔄

Alternative Approaches

Using -replace operator
powershell
$content = Get-Content input.txt -Raw
$content -replace '^(\s*\r?\n)+', '' | Set-Content output.txt
Write-Output "Blank lines removed using -replace"
This reads the whole file as one string and removes blank lines with regex; good for small files but uses more memory.
Using ForEach-Object with condition
powershell
Get-Content input.txt | ForEach-Object { if ($_.Trim() -ne '') { $_ } } | Set-Content output.txt
Write-Output "Blank lines removed using ForEach-Object"
Explicitly checks each line and outputs only non-blank lines; slightly more verbose but clear.

Complexity: O(n) time, O(n) space

Time Complexity

The script reads each line once and filters it, so time grows linearly with the number of lines.

Space Complexity

All lines are stored in memory during processing, so space grows linearly with file size.

Which Approach is Fastest?

The pipeline with Where-Object is efficient and readable; regex approach uses more memory but can be faster for small files.

ApproachTimeSpaceBest For
Where-Object filterO(n)O(n)Large files, readability
-replace regexO(n)O(n)Small files, regex familiarity
ForEach-Object conditionO(n)O(n)Explicit control, clarity
💡
Use Trim() to remove spaces before checking if a line is blank.
⚠️
Forgetting to use Trim() causes lines with spaces to be treated as non-blank.