0

I want to open a text file located on a network drive with notepad++ in Windows PowerShell.

I create the following PS1 file

Set-Location -Path "C:\Program Files\Notepad++\"
$Targetfile="\\server\path\myfile.txt"
.\Notepad++.exe $Targetfile

Error message : cannot find file

Can I run the following command in Windows PowerShell(Run exe file with path in PowerShell)

.\"C:\Program Files\Notepad++\notepad++.exe" "\\server\path\myfile.txt"
mklement0
  • 382,024
  • 64
  • 607
  • 775
Anson
  • 243
  • 1
  • 5
  • 20
  • 2
    "Can I run" - try it and you will see if it works. The notation ```.\abc``` means means "abc in the current location". If `Notepad++.exe` isn't in the current directory, then of course you will get a "file not found" message. – Bill_Stewart Jul 17 '21 at 01:00

1 Answers1

0

Note:

  • The answer below contains general information about invoking executables from PowerShell.

  • Your specific problem may be one of the following:

    • Executable Notepad++.exe may be missing from directory C:\Program Files\Notepad++

    • The target file may not exist or the server may not be reachable.

Note that prefixing an executable path with .\ (which refers to a file in the current directory) if that path is a full (absolute) path (as in your 2nd attempt.,
.\"C:\Program Files\Notepad++\notepad++.exe" ) is both logically pointless and fails in practice.


If Notepad++.exe is in one of the directories listed in the $env:PATH environment variable:

# Note: NO ".\" prefix, which is only needed to invoke an executable
#       located in the *current directory*.
Notepad++.exe $TargetFile

If you need to reference it by its full path, there is no need to use Set-Location followed by a .\-prefixed invocation (unless you truly need the working directory to be Notepad++'s installation directory).

To call it by its full path directly, use &, the call operator:

& "C:\Program Files\Notepad++\notepad++.exe" $TargetFile

Note that & is required in this case, because your executable path is quoted, of necessity, due to containing embedded spaces. As the first command above shows, & is optional if the executable name or path is unquoted (and contains no variable references) - see this answer for more information.

mklement0
  • 382,024
  • 64
  • 607
  • 775