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"
1
  • 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. Commented Jul 17, 2021 at 1:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.