How can I open up a file in Notepad++ from the Powershell command line?
-
2@martineau: Actually this question is quite helpful for opening any file in Notepad++ from PowerShell. Just sayin..Scotty.NET– Scotty.NET2015-04-14 15:03:26 +00:00Commented Apr 14, 2015 at 15:03
-
1Can we just edit this canonical question to not be Python specific? It has nothing to do with python...Kellen Stuart– Kellen Stuart2016-10-03 19:48:36 +00:00Commented Oct 3, 2016 at 19:48
8 Answers
Inside PowerShell I can simply use the start and get general results
to open a python file with notepad++ here is what I did.
Start notepad++ ex1.py
this will start notepad++ and load the file ex1.py assuming you are in the same directory as the .py file. You can change that by adding the full path name
start notepad++ c:\users\you\desktop\files\ex1.py
4 Comments
Start opens any .exe program with any argument.Get-Content "ids_2020-02-01.jsonl" -Tail 100 | Start notepad++ to no availnotepad++ -openFoldersAsWorkspace . (This will open the current directory as workspace.. Replace . with the path to the another directory if you want)notepad++, you have to use an array for the 2nd argument of Start: start notepad++ "c:\users\you\desktop\files\ex1.py","-n100" (open ex1.py on line number 100) -- OR -- more explicitly: start notepad++ @("c:\users\you\desktop\files\ex1.py","-n100")Because the default path contains spaces, you have to quote the path to the exe. However because PowerShell is also a scripting language. A string by itself is simply evaluated as a string e.g.:
C:\ PS> 'Hello world'
Hello world
So you have to tell PowerShell you want to invoke the command that is named by the string. For that you use the call operator & e.g.:
C:\ PS> & 'C:\Program Files (x86)\Notepad++\notepad++.exe'
or if notepad++ is in your path:
C:\ PS> notepad++
or if you're in the same dir as the exe:
C:\ PS> .\notepad++
1 Comment
To extrapolate on the previous answers and tie them up in a tidy bow: If you want to open a file with spaces in the path or name:
. 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
or
& 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
It can also be set it as an alias:
Set-Alias -Value 'C:\Program Files (x86)\Notepad++\notepad++.exe' -Name 'NotePad'
$FileWithSpaces = 'C:\T e m p\File With Spaces.txt'
NotePad $FileWithSpaces
The top line here can be copied into (one of) your $Profile .ps1 file(s) so you don't need to keep using Set-Alias in every new PS instance.
2 Comments
I know this is an old question, but I found a bit of a workaround, quite by accident, and it is extremely straightforward. If you install and maintain Notepad++ via Chocolatey (think apt-get for Windows, but built on top of NuGet), then you get a shim that can be invoked from the command line.
cinst notepad++
And even if you already have an existing installation of Notepad, you can still "install" it from Chocolatey, and it will pull in the existing installation and maintain it.
I use Chocolatey for as much as I possibly can, because you can update everything in one fell swoop.
After that, editing things from PowerShell is a snap. Like my PowerShell profile:
notepad++ $PROFILE
Hope this helps someone, or several someones!
Comments
In my case, I wanted to start Notepad++ with a file as an argument, and open as admin. I wanted to open one of the PowerShell profiles. I had to use the following command variation:
start-process -Verb runas -filepath "C:\Program Files (x86)\Notepad++\notepad++.exe" "`"$($PROFILE.AllUsersAllHosts)`""
All the other variations didn't work, I think due to a space in the path of the file to be opened. So, you must escape the " as:
"He said `"This is fun.`""
Comments
To pass multiple arguments to Notepad++, you have to use an array for the 2nd argument of Start:
Start notepad++ "C:\Users\user\Documents\PS Scripts\script.ps1","-n100"
More explicitly:
Start notepad++ @("C:\Users\user\Documents\PS Scripts\script.ps1","-n100")
Which is equivalent to typing this in a command line directly:
notepad++ "C:\Users\user\Documents\PS Scripts\script.ps1" -n100
This will open script.ps1 file on Line Number 100.