How to suppress the Press any key to continue prompt in PowerShell

Anant Bera 251 Reputation points
2023-12-29T04:55:55.8+00:00

Hello Experts,

We've developed a PowerShell script to offboard devices from the Microsoft Defender portal using a CMD command. The PowerShell script provides a GUI where we can select the CMD file and execute it. When manually running the CMD command, a prompt appears with "Press any key to continue." After pressing a key, the CMD command closes. In the PowerShell GUI script, we attempted to suppress the new window using arguments like NoNewWindow and passthrough. Although the code executes successfully, it gives us an exit code of 1, whereas manual execution provides an exit code of 0.

How can we suppress the "Press any key to continue" prompt to ensure the code executes with an exit code of 0?

Thanks.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Dikky Ryan Pratama 1,470 Reputation points
    2023-12-29T06:20:08.8866667+00:00

    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:

    1. 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 the Start-Process cmdlet.
      
          Start-Process -FilePath "yourcmdfile.cmd" -NoNewWindow -Wait
      
      
      The -Wait parameter ensures that PowerShell waits for the CMD process to finish before continuing.
    2. Use Invoke-Expression: Another approach is to use Invoke-Expression (or iex for short) to run the CMD command, again with the -NoNewWindow parameter.
      
          Invoke-Expression -Command "cmd.exe /c yourcmdfile.cmd" -NoNewWindow
      
      
      The /c option in cmd.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.

    4 people found this answer helpful.

  2. MotoX80 36,416 Reputation points
    2023-12-29T14:38:41.1766667+00:00

    Remove the pause statement from your bat/cmd file.

    User's image

    No pause, no "Press any key to continue" prompt.

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.