简体   繁体   中英

PowerShell Start-Process with -ArgumentList doesn't work

A very easy line in a powershell script

Start-Process -FilePath displayswitch -ArgumentList "/extend"

It is supposed to extend my screen. Yet it doesn't work because there is something wrong with the ArgumentList. What is it?

In general, do not use Start-Process to invoke CLIs (command-line interfaces; ie, programs that accept command-line arguments), unless you explicitly want to launch them asynchronously, in a new window - see this answer for background information.

In the case at hand - for reasons unknown to me [1] - Start-Process additionally seems to prevent the displayswitch.exe executable from functioning properly when arguments are passed (the syntax of your Start-Process command is correct).

However, that is a moot point if you invoke displayswitch.exe directly , which is generally the right choice for CLIs: [2]

displayswitch.exe /extend

[1] The problem may be related to the fact that displayswitch.exe is a GUI-subsystem rather than a console-subsystem application. That is, it doesn't allocate a console window for itself and operates asynchronously when called from an existing console window. Using Start-Process therefore makes the executable run without a console window. That said, running displayswitch.exe /extend from the Windows Run dialog ( WinKey-R ), where no console window is involved either, works correctly.

[2] Given that displayswitch.exe is a GUI-subsystem application, it will act asynchronously even in direct invocation, but I assume that's not a problem here. If your Start-Process command had worked, it would have been asynchronous too. Adding -Wait to a Start-Process call makes it synchronous, and you can emulate this behavior in direct invocation by piping to Out-Null . However, due to how displayswitch.exe is implemented, even displayswitch.exe /extend | Out-Null displayswitch.exe /extend | Out-Null doesn't result in fully synchronous operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM