How to Remove Blank Lines from an Array in PowerShell?

Are you looking to remove blank lines from a PowerShell array? In this PowerShell tutorial, I will explain how to remove blank lines from an array in PowerShell.

To remove blank lines from an array in PowerShell, you can use the Where-Object cmdlet with a condition to filter out empty or whitespace-only strings. For example, $array = $array | Where-Object { $_.Trim() -ne ” } will remove all the blank lines from $array. This approach checks each element, trims any whitespace, and ensures only non-empty strings remain in the array.

Remove Blank Lines from an Array in PowerShell

An array is a data structure that can store a collection of items. In PowerShell, arrays can hold items of any type, including strings, integers, and even other arrays. Arrays are incredibly useful for storing and manipulating sets of data.

A “blank line” in an array is typically represented by an empty string, which can be denoted as "" or a string that consists only of whitespace characters such as spaces, tabs, or new lines. Identifying these correctly is important to ensure they are removed from the PowerShell array.

Now, let us see 3 methods to remove blank lines from an array in PowerShell. All the scripts I have executed using Visual Studio Code.

Method 1: Using the Where-Object Cmdlet

One of the simplest ways to remove blank lines from an array is by using the Where-Object cmdlet in PowerShell. This cmdlet allows you to filter out items based on a script block that returns True or False.

Here is a complete example of removing blank lines from a PowerShell array.

# Create an array with blank lines
$arrayWithBlanks = @("Line1", "", "Line2", " ", "Line3")

# Remove the blank lines
$cleanArray = $arrayWithBlanks | Where-Object { $_.Trim() -ne "" }

# Display the clean array
$cleanArray

In the above example, $_ represents each item in the array, and the .Trim() method is used to remove any leading or trailing whitespace characters. The comparison -ne "" checks if the trimmed item is not an empty string.

You can check out the output in the screenshot below:

remove blank lines from array powershell

Method 2: Using the foreach Loop

Another method to remove blank lines in PowerShell is by using a foreach loop to iterate over each item in the array and collect the non-empty items into a new array.

Here is the full PowerShell script.

# Create an array with blank lines
$arrayWithBlanks = @("Line1", "", "Line2", " ", "Line3")

# Create an empty array to hold non-empty lines
$cleanArray = @()

# Loop through each item and add non-empty lines to the clean array
foreach ($line in $arrayWithBlanks) {
    if ($line.Trim() -ne "") {
        $cleanArray += $line
    }
}

# Display the clean array
$cleanArray

This method gives you more control over the process and can be easier for beginners to understand, as it breaks down the steps of filtering the array. Check out the screenshot below for the output:

remove empty lines from array powershell

Method 3: Using Regular Expressions with the -match Operator

PowerShell also supports regular expressions, which can be used to identify and remove blank lines using the -match operator.

Here is a complete example of how to use regular expressions to remove blank lines from a PowerShell array.

# Create an array with blank lines
$arrayWithBlanks = @("Line1", "", "Line2", " ", "Line3")

# Use regex to remove blank lines
$cleanArray = $arrayWithBlanks | Where-Object { $_ -match "\S" }

# Display the clean array
$cleanArray

In this example, \S is a regex pattern that matches any non-whitespace character. The -match operator returns True for any string that contains at least one non-whitespace character, effectively filtering out blank lines.

Here is the output in the below screenshot.

powershell remove empty lines from string array

Conclusion

Removing blank lines from an array in PowerShell can be accomplished using various methods like the Where-Object cmdlet is straightforward and concise, the foreach loop offers clear logic and control, and regular expressions provide powerful pattern matching capabilities.

In this PowerShell script tutorial, I have explained how to remove blank lines from an array in PowerShell using various methods and examples.

You may also like: