Given is a text file, say
my_text_file.txt. How to modify its content in your Windows PowerShell working directory?
I’ll start with the most direct method to solve this problem in 90% of cases and give a pure PowerShell method afterward.
By the way, you can learn all about becoming a PowerShell developer in this blog post.
Method 1: Using Notepad
The easiest way to edit a text file in PowerShell on your Windows machine is to run the command
notepad.exe my_text_file.txt, or simply
notepad my_text_file.txt, in your PowerShell terminal to open the text file with the visual editor Notepad.
You can also skip the
.exe prefix in most cases:
Now, you may ask:
💡 Is Notepad preinstalled in any Windows installation? The answer is: yes! Notepad is a generic text editor to create, open, and read plaintext files and it’s included with all Windows versions.
Here’s how that looks on my Win 10 machine:
When I type in the command
notepad.exe my_text_file.txt, PowerShell starts the Notepad visual editor in a new window.
I can then edit the file and hit
CTRL + S to save the new contents.
But what if you cannot open a text editor—e.g. if you’re logged into a remote server via SSH?
Method 2: Pure PowerShell Approach
If you cannot open Notepad or other visual editors for some reason, a simple way to overwrite a text file with built-in PowerShell tools is the following:
- Run the command
echo 'your new content' > my_text_file.txtto print the new content using
echoand pipe the output into the text file
- Check the new content using the command
PS C:\Users\xcent\Test> echo 'hello universe!' > .\my_text_file.txt PS C:\Users\xcent\Test> cat .\my_text_file.txt hello universe!
Here’s what this looks like on my computer where I changed
my_text_file.txt to contain the text
This is a simple and straightforward approach for small changes. However, if you have a large file and you just want to edit some minor details, this is not the best way.
Method 3: Install and Use Nano Text Editor
If you need a full-fledged solution to edit, potentially large, files in your Windows PowerShell, you need to do a quick installation.
I recommend the widely use Nano text editor for Windows machines. Here’s how it looks generally:
You can install the Nano text editor using the following approach outlined here.
First, install Chocolatey, a Windows package management solution, on your computer. This makes sure that you install trustworthy software packages from trustworthy sources. Download the latest version here.
Second, run the following choco install command in your PowerShell to install the
choco install nano -y
Now, you can open and edit your file in your PowerShell using this command:
👉 Recommended: How to Edit a Text File in CMD (Windows)
Method 4: If you SSH’d to a Unix Machine
Of course, if you have logged in a Unix-based machine, you don’t need to install any editor because it comes with powerful integrated editors such as vim or emacs.
One of the following three commands should open your file in a terminal-based editing mode:
vim my_text_file.txt vi my_text_file.txt emacs my_text_file.txt
You can learn more about Vim here.
Method 5: New-Item and Add-Content CMDlets
To add a text file in PowerShell, you can use the
New-Item cmdlet with the
-ItemType parameter set to
Here are the steps:
- Open PowerShell by typing “
powershell” in the search bar or press the
Windows key + Xand choose “Windows PowerShell” from the menu.
- Navigate to the directory where you want to create the file using the
cdcommand. For example, if you want to create the file in the Documents folder, type
- Type the following command to create a new text file:
New-Item -ItemType File -Path "filename.txt"
filename” with the desired name of your text file.
Enterto execute the command. This will create a new text file in the directory you navigated to.
- To add content to the text file, you can use the
Add-Contentcmdlet. For example, to add the text
"Hello, World!"to the file, type the following command:
Add-Content -Path "filename.txt" -Value "Hello, World!"
Enterto execute the command. This will add the text
"Hello, World!"to the text file.
You can now view the text file by opening it in a text editor or by using the
Get-Content cmdlet in PowerShell.
To edit a
file.txt in PowerShell, use the command
notepad file.txt to open a graphical editor on Windows.
If you need a simple file edit in your terminal without a graphical editor and without installation, you can use the command
echo 'new content' > file.txt that overwrites the old content in
If you need a more advanced terminal-based editing approach, install the Nano text editor and run
nano file.txt to open the file in editing mode.
If you’re SSH’d into a Unix machine, running the Vim console-based editor may be the best idea. Use
vim file.txt or
vi file.txt to open it.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.