Hi Anant Bera,
To suppress the "Press any key to continue" prompt in a CMD command executed from PowerShell, you can use the following methods:
- Use the
-NoNewWindow
Parameter: When calling the CMD command from PowerShell, use the-NoNewWindow
parameter to suppress the creation of a new window. This can be achieved using theStart-Process
cmdlet.
TheStart-Process -FilePath "yourcmdfile.cmd" -NoNewWindow -Wait
-Wait
parameter ensures that PowerShell waits for the CMD process to finish before continuing. - Use
Invoke-Expression
: Another approach is to useInvoke-Expression
(oriex
for short) to run the CMD command, again with the-NoNewWindow
parameter.
TheInvoke-Expression -Command "cmd.exe /c yourcmdfile.cmd" -NoNewWindow
/c
option incmd.exe /c
is used to carry out the command specified by the string and then terminate.
Ensure that you replace "yourcmdfile.cmd"
with the actual path to your CMD file.
Here's an example combining both methods:
$cmdFilePath = "C:\Path\To\Your\Script.cmd"
# Method 1: Start-Process with -NoNewWindow
Start-Process -FilePath $cmdFilePath -NoNewWindow -Wait
# Method 2: Invoke-Expression with -NoNewWindow
Invoke-Expression -Command "cmd.exe /c $cmdFilePath" -NoNewWindow
Choose the method that works best for your scenario, and it should suppress the "Press any key to continue" prompt and provide an exit code of 0. If you're still facing issues with exit code 1, you may want to check the CMD script for any error conditions that could be causing it.
Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.
Regards.