I'm trying to find a way to get PowerShell not to spawn a command window when running an executable using Start-Process
.
If I call the executable directly within the script (e.g. .\program.exe
) then the program runs (with its arguments) and the output is returned to the PowerShell window.
If I use Start-Process
the program spawns a command window where the program runs and returns it's output.
If I try and use the -NoNewWindow
switch of Start-Process
the script then errors out saying it can't find the exe file.
I would prefer to use Start-Process
to have access to the -Wait
switch, as the programs and configurations the script makes can take some time individually to finish, and I don't want later commands starting up.
This code runs the executable in a separate command window:
Start-Process DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait
This code runs the exe within the PowerShell console:
.\DeploymentServer.UI.CommandLine.exe download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime
If I add the -NoNewWindow to the Start-Process code
Start-Process DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow
I get the following error:
Start-Process : This command cannot be executed due to the error: The system cannot find the file specifie At C:\Temp\SOLUS3Installv1.3.ps1:398 char:22 + Start-Process <<<< DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
(Start-Process .\program.exe -NoNewWindow -PassThru).WaitForExit()
– Mathias R. Jessen