Edit a text file on the console using Powershell
Asked Answered
B

21

155

I'm trying to figure out the easiest way to edit text files in the console (PowerShell in my case). I'm using Windows 7 64 bit. It galls me that I can't just type edit filename.txt to edit a file. That used to work, but that's all changed. What are my options to view and edit text files within the windows console, and if you tell me to install and learn VIM I'm going to punch you in the face. :-)

Brigidbrigida answered 15/6, 2012 at 5:30 Comment(3)
I took the liberty of editing your question because it has nothing at all to do with PowerShell.Coranto
use nano ......Phototransistor
How do I edit text files in the Windows command prompt?Jorge
N
50

Why not use notepad?

notepad.exe filename.txt

The old edit.com works in PowerShell (at least on my box: Windows 7 Pro x86) but in x64 it doesn't work due to its 16bit architecture.

You can take a look at this easy editor.

Nodical answered 15/6, 2012 at 5:34 Comment(5)
If you're trying to edit a file locally you could just type notepad instead of notepad.exe and it will work fine. You just saved a few seconds each day :)Brattice
notepad fails silently for me (Windows2019). Does it really have a console editor built in, or is it trying to open a window in a windowless session?Plunk
This answer requires a GUI and therefore does not work with Windows Core (e.g. container, ps-remoting, AWS SSM Start-Session)Hagerty
@Thomas Yeah, and it wouldn't work from the Windows safe mode without a GUI.Ibert
Notepad fails to handle UNIX newlines.Radial
S
29

Kinesics Text Editor.

It's super fast and handles large text files, though minimal in features. There's a GUI version and console version (k.exe) included. Should work the same on linux.

Example: In my test it took 7 seconds to open a 500mb disk image.

screenshot

Shortridge answered 11/10, 2013 at 9:40 Comment(1)
You don't have to install. Just run the .exe package and it will pause with error 740 (requires elevation). Take note of the directory where it extracted the files, and copy them somewhere.Enforce
R
28

While risking you punching me, I guess you are stuck with the solution you mentioned. Have a look at this posting on SuperUser:

Which are the non-x text editors in Powershell?

Also, there is a nano version for windows:

Nano Editor

I'll duck and cover now, hopefully someone will have a more sufficient answer.

Rankins answered 15/6, 2012 at 5:39 Comment(2)
For users who need to know how to get nano working from every directory the relevant google search is: google.co.uk/… I personally prefer nano to kinesics but individual preferences may varyCollusive
You saved my day. There is a windows version in win32-support.Yulan
C
23

Bit of a resurrect but for anyone else coming to this question, take a look at the Micro editor. It's a small standalone EXE with no dependencies and with native Windows 32\64 versions. Works well in both PowerShell and CMD.EXE.

enter image description here

Cyril answered 16/5, 2017 at 11:45 Comment(2)
micro.exe process chinese characters better than k.exe / kit.exe.Calla
but sadly, micro.exe is not good at processing windows file path, the "open file" command doesn't provide a file explorer window.Calla
T
22

I agree with Sven Plath. Nano is a great alternative. If you have Chocolatey or scoop, you can install nano by typing the following in Powershell:

PS C:\dev\> choco install nano
# --OR--
PS C:\dev\> scoop install nano

Then, to edit somefile.txt enter:

PS C:\dev\> nano somefile.txt

It's pretty neat!

Edit: Nano works well on my Windows 10 box but takes incredibly long to load the first time on my Windows 7 machine. That made me switch to vim (vi) on my Win 7 laptop

PS C:\dev\> choco install vim
PS C:\dev\> vim $profile

Add a line in the powershell profile to Set-Alias (sal)

sal vi vim

Esc - : - x - Enter :-)

Tropo answered 18/8, 2017 at 19:31 Comment(0)
F
17

If you use Windows container and you want change any file, you can get and use Vim in Powershell console easily.

To shelled to the Windows Docker container with PowerShell:

docker exec -it <name> powershell

  • First get Chocolatey package manager

    Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression;

  • Install Vim

    choco install vim

  • Refresh ENVIRONMENTAL VARIABLE You can just exit and shell back to the container

  • Go to file location and Vim it vim file.txt

Fathomless answered 10/4, 2019 at 15:6 Comment(2)
This was extremely helpful for the exact need of I was within a Windows docker container. I was able to install choco and then install nano. Will keep the WebRequest command in my back pocket.Gregson
By using choco install -y vim the license prompts are skipped; makes it even more scriptable. :)Slapstick
C
10

You could install Far Manager (a great OFM, by the way) and call its editor like that:

Far /e filename.txt
Coranto answered 15/6, 2012 at 5:51 Comment(1)
thanks for the file manager, it's a long time from the last time i used this type of file manager, Ohh, I remember the times playing on school old PC :)Grumpy
H
7

You can install nano in powershell via choco - It's a low friction way to get text editing capabilities into powershell:

  1. Install Choco
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  1. Install Nano
choco install nano
  1. Profit
nano myfile.txt

Best part is it becomes part of the path, and stays working across reboots etc :)

Hisakohisbe answered 24/10, 2019 at 20:49 Comment(3)
Nano on windows in remote powershell doesn't seem to recognize up/down/left/right keys.Badmouth
You should probably use a more recent version of nano. The one provided by choco is very old. Then in order to use it with SSH, you need to run with -cg flags but will lose the cursor update, anyway. To update visual position of cursor location, press CTRL-L.Badmouth
If you have any problems with nano, use vim insteadDoscher
M
6

install vim from online, and then you can just do: vim "filename" to edit that file

Mim answered 28/2, 2014 at 5:16 Comment(2)
Crappy answer, after the OP specifically stated they did not want to use Vim. -1.October
Not a crappy answer because Google will lead people other than OP here.Ibrahim
A
4

I'm thinking you could just use notepad, like this:

notepad myfile.extension

It should open in notepad.

Alphitomancy answered 4/9, 2017 at 20:29 Comment(1)
what is the difference to the already existing answer https://mcmap.net/q/156088/-edit-a-text-file-on-the-console-using-powershell ?Dilapidated
V
3

I am a retired engineer who grew up with DOS, Fortran, IBM360, etc. in the 60's and like others on this blog I sorely miss the loss of a command line editor in 64-bit Windows. After spending a week browsing the internet and testing editors, I wanted to share my best solution: Notepad++. It's a far cry from DOS EDIT, but there are some side benefits. It is unfortunately a screen editor, requires a mouse, and is consequently slow. On the other hand it is a decent Fortran source editor and has row and column numbers displayed. It can keep multiple tabs for files being edited and even remembers where the cursor was last. I of course keep typing keyboard codes (50 years of habit) but surprisingly at least some of them work. Maybe not a documented feature. I renamed the editor to EDIT.EXE, set up a path to it, and invoke it from command line. It's not too bad. I'm living with it. BTW be careful not to use the tab key in Fortran source. Puts an ASCII 6 in the text. It's invisible and gFortran, at least, can't deal with it. Notepad++ probably has a lot of features that I don't have time to mess with.

Vauban answered 9/3, 2018 at 6:35 Comment(0)
S
3

Sadly powershell doesn't come with a built in console-text editor. You can redirect standard input for simple oneliners like so:

# write text and overwrite the file with that text
"my text that will appear in the file" > file.txt

For anything more complicated you will need a package manager Chocolatey

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

and install any of the following editors:

Nano

choco install nano

Issues

  • Very laggy on windows
  • Problems with arrow control

Vim

choco install vim

Issues

  • No major issues just remember esc + :!qa will exit vim
Sampling answered 22/4, 2022 at 6:44 Comment(0)
J
2

I had to do some debugging on a Windows Nano docker image and needed to edit the content of a file, who would have guessed it was so difficult.

I used a combination of Get-Content and Set-Content and base 64 encoding/decoding to update files. For instance

Editing foo.txt

PS C:\app> Set-Content foo.txt "Hello World"                                                                                                                                                                                        
PS C:\app> Get-Content foo.txt                                                                                                                                                                                                      
Hello World                                                                                                                                                                                                                         
PS C:\app> [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("TXkgbmV3IG11bHRpDQpsaW5lIGRvY3VtZW50DQp3aXRoIGFsbCBraW5kcyBvZiBmdW4gc3R1ZmYNCiFAIyVeJSQmXiYoJiopIUAjIw0KLi4ud29ybGQ=")) | Set-Content foo.txt 
PS C:\app> Get-Content foo.txt                                                                                                                                                                                                      
My new multi                                                                                                                                                                                                                        
line document                                                                                                                                                                                                                       
with all kinds of fun stuff                                                                                                                                                                                                         
!@#%^%$&^&(&*)!@##                                                                                                                                                                                                                  
...world                                                                                                                                                                                                                            
PS C:\app>       

The trick is piping the base 64 decoded string to Set-Content

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("...")) | Set-Content foo.txt 

Its no vim but I can update files, for what its worth.

Juridical answered 1/11, 2017 at 18:0 Comment(1)
Set-Content overwrites anything that is already in the file.Malchus
S
2

Not sure if this will benefit anybody, but if you are using Azure CloudShell PowerShell you can just type:

code file.txt

And Visual Studio code will popup with the file to be edit, pretty great.

Semester answered 25/10, 2020 at 9:47 Comment(1)
Worked for me in Win 10 as well, working with Windows PowerShell. Thanks.Gendarmerie
D
2

If you have windows subsystem for linux (wsl), you will find the following command very useful:

bash -c "vi filename.txt"
Dustheap answered 16/6, 2021 at 1:16 Comment(0)
A
1

Well there are thousand ways to edit a Text file on windows 7. Usually people Install Sublime , Atom and Notepad++ as an editor. For command line , I think the Basic Edit command (by the way which does not work on 64 bit computers) is good;Alternatively I find type con > filename as a very Applaudable method.If windows is newly installed and One wants to avoid Notepad. This might be it!! The perfect usage of Type as an editor :)

reference of the Image:- https://www.codeproject.com/Articles/34280/How-to-Write-Applet-Code

Arrio answered 31/10, 2017 at 9:50 Comment(0)
V
1

In linux i'm a fun of Nano or vim, i used to use nano and now vim, and they are really good choices. There is a version for windows. Here is the link https://nano-editor.org/dist/win32-support/

However more often we need to open the file in question, from the command line as quick as possible, to not loose time. We can use notepad.exe, we can use notepad++, and yea, we can use sublim text. I think there is no greater then a lightweight, Too powerful editor. Sublime text here. for the thing, we just don't want to get out of the command line, or we want to use the command line to be fast. and yea. We can use sublime text for that. it contain a command line that let you quickly open a file in sublime text. Also there is different options arguments you can make use of. Here how you do it.

First you need to know that there is subl.exe. a command line interface for sublim.

enter image description here

1-> first we create a batch file. the content is

enter image description here

@ECHO OFF

"C:\Program Files\Sublime Text 3\subl.exe" %*

We can save that wherever we want. I preferred to create a directory on sublime text installation directory. And saved there the batch file we come to write and create.

(Remark: change the path above fallowing your installation).

2-> we add that folder to the path system environment variable. and that's it.

enter image description here

or from system config (windows 7/8/10)

enter image description here

then:

enter image description here

then:

enter image description here

then we copy the path:

enter image description here

then we add that to the path variable:

enter image description here

too quick!

launch a new cmd and now you've got subl command working well!

enter image description here

to open a file you need just to use subl command as fellow:

subl myfileToOpen.txt

you can also use one of the options arguments (type --help to see them as in the image above).

Also note that you can apply the same method with mostly any editor of your choice.

Venous answered 13/12, 2017 at 0:11 Comment(1)
If you're using powershell as your shell instead of cmd, you can just add Set-Alias subl 'C:\Program Files\Sublime Text 3\sublime_text.exe' to your $PROFILE, more info on profile here technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspxSubtilize
W
1

You can do the following:

bash -c "nano index.html"

The command above opens the index.html file with the nano editor within Powershell.

Alternatively, you can use the vim editor with the following command

bash -c "vi index.html"
Wow answered 15/9, 2020 at 12:1 Comment(1)
tested and works on 64 bit windows. I think you need WSL enabled too for bash to be availableDragoman
D
1

Best solution IMHO is to use Start <your editor application> <file to edit>.

For example, if you want to edit file.txt in your current folder with Notepad++, you'll run:

Start Notepad++ .\file.txt

Or for using Notepad just replace "Notepad++" with "Notepad". It works with "Sublime Text" or any other installed editor. Only caveat is that you need to use the app name as installed on your machine.

Demarcusdemaria answered 15/1, 2024 at 20:21 Comment(0)
M
0

Note: This is nowhere near as sophisticated as a terminal text editor like nano or vim, but it's native to PowerShell.

If you want to be able to be able to do some basic file information such as adding new lines or reading the contents, PowerShell has the following options:

  • Get-Content,
  • Add-Content, and
  • Set-Content

Get-Content reads the contents of the file and returns it to the PowerShell terminal. Add-Content appends a new line the file, preserving existing information. Set-Content replaces the contents of the file.

If you use

Add-Content -Path "Path/To/File"

it will return

cmdlet Add-Content at command pipeline position 1
Supply values for the following parameters:
Value[0]: 

and you can enter whatever information you want on that line. You can keep adding new lines as long as you don't submit an empty line. To "save and exit", press enter twice (to submit a blank line).

Malchus answered 29/3, 2024 at 0:40 Comment(0)
H
-1

You can set an alias for edit with your preferred editor.

Open shell profile

notepad $profile

Add the Set-Alias command

Set-Alias edit 'C:\Program Files\Sublime Text\subl.exe'

Then on a new shell

edit file.txt
Hypothec answered 2/3, 2023 at 14:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.