

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove empty string/lines from PowerShell?
In many instances, you need to remove empty lines or strings from the PowerShell string array or the files. In this article instead of removing empty string, we will get the result or filter the output from lines that are not empty. In this way, we can get the output without empty lines.
Consider the example below, we have a file called EmptryString.txt and we need to remove empty lines from the content.
The content of the text file is as below.
PS C:\Windows\System32> Get-Content D:\Temp\EmptyString.txt This is example of empty string PowerShell PowerShell DSC String Array Hello
You just need to apply the condition where the line is not empty. See the code below.
Example
Get-Content D:\Temp\EmptyString.txt | where{$_ -ne ""}
Output
This is example of empty string PowerShell PowerShell DSC String Array Hello
Similarly, you can use the above command to remove empty lines from the string array. For example,
$str = "Dog","","Cat","","Camel","","Tiger" $str PS C:\Windows\System32> $str Cat Camel TigerDog
Now apply the same logic to remove the empty lines.
Example
$str | where{$_ -ne ""}
Output
PS C:\Windows\System32> $str | where{$_ ne ""} Dog Cat Camel Tiger
- Related Questions & Answers
- How to Remove Empty Lines from a File on ubuntu
- Remove new lines from a string and replace with one empty space PHP?
- Remove new lines from string in PHP
- How to remove an empty string from a list of empty strings in C#?
- How to remove end lines from a boxplot in R?
- How to remove empty rows from an R data frame?
- Python Program to Remove the nth Index Character from a Non-Empty String
- How to remove grid lines from an image in Python Matplotlib?
- How to retrieve a specific number of lines from files in PowerShell?
- Python | Remove empty tuples from a list
- How to remove empty strings from a list of strings in Python?
- How to remove a member from the local group using PowerShell?
- How to Remove the computer from the AD domain using PowerShell?
- How to filter empty string values from a Java List?
- How to remove lines in a Matplotlib plot?
Advertisements