How to Delete Lines From Text File in PowerShell

Written by keith perry | 13/05/2017
How to Delete Lines From Text File in PowerShell
PowerShell is used to program utility operations in the Windows operating system. (Thinkstock/Comstock/Getty Images)

PowerShell is a scripting language from Microsoft for use with the Windows operating system. The language can be used to generate many different utility programs, limited only to your imagination. Manipulation of files, folders and configuration settings are common tasks. Simple programs can be entered at the Powershell command prompt with more complex programs stored in a file. Simple manipulation of content in a text file can be completed at the command prompt.

Open a text editor and create a new text file with alternating lines of text and blank lines.

Save the text file you created as "C:\file1.txt" and close the text editor.

Open the PowerShell program from the "Start" button on the Windows task bar, select "All Programs," "Accessories" and "Windows PowerShell."

Enter the command "(get-content c:\file1.txt) | where {$_ -né ""} | out-file c:\file1.txt" at the PowerShell command prompt.

Open the text file "C:\file1.txt" with the text editor and note the PowerShell command deleted the blank lines of text.

Things you need

  • PowerShell version 2.0 or later

Show MoreHide

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.