DISCOVER
×

How to Delete Lines From Text File in PowerShell

Updated July 20, 2017

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'll Need

  • PowerShell version 2.0 or later
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Keith Perry has been an employee and contractor for several large companies in various information-technology jobs. He holds an executive Master of Business Administration from Jacksonville University with an undergraduate degree in computer science from University of North Florida. He began writing for Lotus Notes Advisor and Mobile Advisor publications in 1998.