Powershell

Edit a Text File on the Console Using PowerShell

PowerShell is a Windows tool that enables users to perform several automation and administrator tasks. It can perform multiple operations such as deleting, renaming, copying, moving, and editing. Moreover, PowerShell can also edit text files. This task can be performed by installing some of the text editors inside the PowerShell console. For instance, a third-party text editor can also be used for the stated purpose.

This tutorial will overview a detailed procedure to edit a text file using PowerShell.

How to Edit a Text File on the PowerShell Console?

These resources can be utilized in fulfilling the stated query:

Method 1: Use “Notepad” Text Editor to Edit File on the Console

The most convenient way to modify a text file using the “Notepad” editor through PowerShell. To make it possible, execute the “notepad.exe” command alongside the text file path in the PowerShell console:

> notepad.exe C:\Doc\File.txt

After executing the above code, PowerShell will launch the text file in the new notepad window. Now, edit the text file and press “CTRL+S” to save the file.

Method 2: Use the “Echo” Command to Edit the File on the Console

The “echo” command with the combination of the redirect “>” operator can also be used to edit the text file in PowerShell. Execute the given command in the console to edit a text file:

> echo "Yes it is!!!" > C:\Doc\File.txt

According to the above code:

  • First, write the “echo” command, alongside the text you want to write in the file within inverted commas.
  • After that, use the redirect “>” operator and add the path of the file to be edited:

The redirect “>” will replace the text inside the file with the new one.

Execute the “Get-Content” cmdlet along with the text file address to view the edited text within PowerShell console:

> Get-Content C:\Doc\File.txt

Method 3: Use “Vim” Editor to Edit a Text File on the Console

The PowerShell users can also edit the text file using the “Vim” text editor. For that reason, first, install the “Vim” text editor by reading this post. After installing it, make sure to add its path to the Windows “Environment Variables” program. Restart Windows and then launch PowerShell and execute the “vim” command along with the text file path:

> vim C:\Doc\File.txt

After executing the above command, the “VIM” text editor will be launched. Type “i” to start editing the text file:

When you are done with editing the text file, type “:wq” and press the “Enter” button to save the text file and exit from the “Vim” file editor.

Conclusion

To edit a text file on the console using PowerShell, several methods can be utilized, which include “Notepad”, “Echo”, and “Vim” text editor. All these methods open up the text file and then allow you to edit it inside the PowerShell console. This write-up has overviewed a detailed guide to edit a text file using PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.