1

If I cd into a folder where I have an executable binary and type its name in PowerShell, it returns standard output back into PowerShell.

PS C:\Users\User\AppData\Roaming\npm> mybinary This does not open cmd window and executes inside PowerShell window.

But if I try to call from somewhere else, when I am not in the directory where the binary is, it creates a new cmd window and it runs there. Why? I want to run it inside the existing PowerShell window.

PS C:\Users\User> & "C:\Users\User\AppData\Roaming\npm\mybinary" Opens a new cmd window and runs there.

1 Answer 1

1

Where are you running this from? consolehost/ISE/VSCode other?

PowerShell is doing exactly what you asked it to do. What does the executable do? What do you expect to get back? Try your use case with any other built-in Windows Executable.

# Examples: all of which send results back to the PowerShell console without popping a new window.

PS C:\Scripts> & "C:\Windows\system32\nslookup.exe"
# Results
<#
Default Server:  L...
Address:  172...
#>

PS C:\Scripts> nslookup stackoverflow.com
# Results
<#
Server:  L...
Address:  172...1

Non-authoritative answer:
Name:    stackoverflow.com
Addresses:  151...
#>
   
PS C:\Scripts> & 'nslookup' 'stackoverflow.com'
# Results
<#
Server:  L...
Address:  172...

Non-authoritative answer:
Name:    stackoverflow.com
Addresses:  151...
#>

PS C:\Scripts> & "nslookup" "stackoverflow.com"
# Results
<#
Server:  L...
Address:  172...

Non-authoritative answer:
Name:    stackoverflow.com
Addresses:  151....
#>

PS C:\Scripts> & "nslookup stackoverflow.com"
# Results
<#
& : The term 'nslookup stackoverflow.com' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
#>

PowerShell does not run executables, cmd.exe does. Note that the Powershell_ISE actively blocks interactive executables

PowerShell ISE Limitations (Windows) | Microsoft Docs

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt708811(v=vs.85)

$psUnsupportedConsoleApplications
# Results
<#
wmic
wmic.exe
cmd
cmd.exe
diskpart
diskpart.exe
edit.com
netsh
netsh.exe
nslookup
nslookup.exe
powershell
powershell.exe 
#>

Running an executable in the PowerShell console, as long as you pass it all it needs, under the covers calls cmd.exe, cmd.exe runs the executable, and STDOUT is returned to the calling console.

References:

• PowerShell: Running Executables

https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

  1. The Call Operator
&

Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.

In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.

The PowerShell V3.0 parser do it now smarter, in this case you don’t need the & anymore.

Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters

# Example:

& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen

Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths!

With spaces, you have to nest Quotation marks and the result is not always clear!

In this case it is better to separate everything like so:

$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'
 
& $CMD $arg1 $arg2 $arg3 $arg4
 
# or same like that:
 
$AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs

Via VSCode, you can get different responses depending on whether you are using VSCodes' integrated console (the ISE-like environment) or the consolehost (the normal PowerShell console).

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

1 Comment

"PowerShell is doing exactly what you asked it to do" - I don't see how I asked PowerShell to behave differently simply based on whether I execute the exact same file with a relative or absolute path. ..\..\..\..\foo\program param is run synchronously in the PowerShell window while C:\foo\program param is run asynchronously in a cmd popup window. This does not make sense. And & does not change the behavior (PS6).

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.