Open Notepad++ from PowerShell - powershell

How can I open up a file in Notepad++ from the Powershell command line?

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

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++

To open Notepad++ with and create a new empty file in the current path
start notepad++ newFile.txt
To open Notepad++ with an existing file
start notepad++ apples.txt
To specify the path and open multiple files
start notepad++ fruits/apples.txt, fruits/oranges.txt, package.json

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.

Edit your profile and add an alias
Set-Alias -name 'npp' -value 'C:\Program Files\Notepad++\notepad++.exe'
Then:
npp c:\temp\test.txt
Edit your profile:
npp $profile
etc

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!

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.`""

Related

How to call msys2 find command from PowerShell with correct shell escape characters?

I'm wondering how can I escape the following command from PowerShell so that it works?
PS C:\Users\buster\Documents\> find -name \*.c
PowerShell says: error not found *.c
PS C:\Users\buster\Documents\> find -name *.c
PowerShell says: error not found *.c
If you used find like that (without the full path) you most likely used the find.exe that ships with Windows (C:\Windows\system32\find.exe), which is more akin to grep than to Unix find. You get that behavior because Windows searches all directories in $env:PATH for files with the given name (and one of the extensions listed in $env:PATHEXT if no extension was specified), and executes the first match. Since %windir%\system32 is usually at the beginning of the PATH, executables from there take precedence.
You could add C:\msys64\msys64\usr\bin to the beginning of the PATH (before %windir%\system32), although I wouldn't recommend that. A better way would be to define an alias for the command:
New-Alias -Name 'find' -Value 'C:\msys64\msys64\usr\bin\find.exe'
Aliases take precedence over files. You could put the alias definition in your PowerShell profile so that it's automatically loaded whenever you start PowerShell.
Or you could simply use ls -r -fi '*.c' (short for Get-ChildItem -Recurse -Filter '*.c'), which would be the PowerShell way.
Ok false alarm..
Apparently its a case of windows having an executable with the same name as msys2's find.exe under c:\windows\system32 and the windows command getting higher priority in the path list. After explicitly typing out the full path to the msys64 version of find.exe it works.
PS C:\Users\buster\Documents\> C:\msys64\msys64\usr\bin\find -name \*.c
Also, Turns out there's a better way to find *.c files native to cmd.exe that you can call from powershell like this:
PS C:\Users\buster\Documents\> cmd /c dir /S /B *.v

Load script from PowerShell ISE's command environment

Is there a way to open a script file ($profile or otherwise) from within the IDE's command environment (PS> prompt), such that it loads in the IDE itself?
# loads the current user/current host profile in Notepad (probably because .PS1 is associated w/ Notepad.exe)
PS > ii $profile
# same for any random script file
PS > ii .\Foo.ps1
Instead of Invoke-Item, just use ise. When run from the ISE, it will load the file.
PS> ise myscript.ps1
To make Invoke-Item behave like you want (as well as double-clicking from Explorer), you can associate .ps1 files with powershell_ise.exe. Here is a blog post explaining how to do this if you need it.

How to add something to my Powershell profile?

Currently I am trying to make it so I can open up files from PowerShell directly in Sublime Text 3.
I am using this command: Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe'
This works for me initially when I type in "subl filename", but whenever I close out of PowerShell and try it again, it will not work unless I re-type the Set-Alias command. Is there any way to permanently make this possible, so I can always type in subl filename ?
Try running Get-Help about_Profiles
As a quick overview:
PowerShell will load the .ps1 file located at the $Profile path into the session when it starts. Adding Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe' to that file will set that alias every time a new PowerShell session is opened.

adding cygwin bin directory to powershell path environment variable ... cygwin within powershell

Powershell is great for scripting. But when it comes to everyday use, certain things can be a huge PITA!!
so i thought it would be great if i could do something like this in my profile.ps1:
$env:path = "$($env:path);c:\cygwin\bin"
to get access to utilities like tar, zip, etc... but this doesn't work. The variable looks right when i do:
PS > $env:path
but when i try to do, say,
PS > unzip foo.zip
i get a command not found type error.
WTF PowerShell!?
edit: great answers! I looked at it with fresh eyes this morning and realized that I just needed to spell 'cygwin' correctly! now I don't have to switch back and forth between two consoles. It should be noted for anyone who uses this tip that your path in powershell is evaluated in order - if you put c:\cygwin\bin at the end of the $env:path variable, it will be searched last, so it won't interfere with existing powershell aliases / cmdlets.
It worked for me:
To set your profile:
$command = '$env:path = $env:path + ";C:\Program Files\Notepad++"'
$command | Out-File -FilePath $PROFILE -Append -Encoding UTF8
Or just the current shell:
$env:path = $env:path + ";C:\Program Files\Notepad++"
Using $env:path to add the cygwin bin to PATH should work as long as you are trying to use it in the same Powershell session. If you open a new console or if you close and open Powershell, it will not be persisted. Otherwise, what you are doing should work. Make sure you are indeed adding the correct path. If you want to persist the changes, add the line to your $profile.
Also, try using the Mingw / Msys / Msysgit utils. I find Mingw to be more lightweight than cygwin ( if you are using cygwin just to get some of these utils.)
PowerShell by default is only going to modify its local copy of PATH. When you run an external command, they aren't going to see the local environment variables.
Per this TechNet article, you can fall back to the .NET static method SetEnvironmentVariable to do this at the user level if you want this to be a permanent change:
[Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

how to open a folder within Powershell

How to open a folder in PowerShell?
I am not talking on how to start PowerShell in a specific folder.
What I mean is that in the command line of powershell i would like
to open a folder e.g: " open document"
Use the Invoke-Item cmdlet, or its alias: ii.
PS> ii c:\windows # open the windows directory in windows explorer
PS> ii c:\book.xls # open book.xls in Excel
PS> ii . # open the current directory in windows explorer
For Powershell and cmd compatible way ( and I think the most common way):
start .
start c:\
To open the current folder within the powershell type:
PS>> explorer.exe $(pwd)
Use Invoke-Item, alias ii:
ii d:\temp\
you can use the explorer.exe to open the folder:
explorer.exe c:\temp\
explorer.exe <YourFolderPathHere>
Just to add as well to the mix:
PS C:\> ii -path c:\directory\directory\directory
If your file name has two words with a separation consider single quotation marks:
PS C:\> ii -path 'c:\directory\directory\directory directory\'
The following work [note -path is optional]
1. ii or invoke-item
2. explorer.exe
3. start
I realize the question is old but folks finding this via google may find this useful even now:
I created a cmd script with:
#REM Open directory
#REM Version 1.0
#echo off
if [%1]==[] (powershell ii .
) Else (
powershell ii %1
cd %1
)
This will also open a document such as a text file or a MS Word document, as well as opening a folder.
Putting a dot after explorer.exe will open the current directory:
explorer.exe .
I don't exactly understand what you want, but I have two possible solutions:
explorer .\Documents
or
cd .\Documents
As 'open .' in mac will open the current directory, 'start .' in window PowerShell will do the same.
another variant
hh c:\
hh http://stackoverflow.com/questions/8471106
hh $env:windir\help\WindowsPowerShellHelp.chm