1

I tried running the command:

start /B cmd /C " powershell "Invoke-WebRequest -Uri "link here" -OutFile "%USERPROFILE%\Contacts\file.exe""&&start %USERPROFILE%\Contacts\file.exe"&& exit

But for some reason it executes the powershell script in the same window. So when I run this command it show me download bytes and after that process is finished and the file is opened then cmd can close. But I want to run that whole script in the background so I just open cmd run this command and it immediately closes and runs the command in the background.

4
  • 2
    start /? says about /B switch: Start application without creating a new window. Commented Sep 12, 2022 at 20:05
  • Yes, that's what I want to do. I want it to create a new cmd window thats invisible and close the current one. And in the newly opened invisible window I want to execute a powershell script to download a file and then open it. Commented Sep 12, 2022 at 20:09
  • 1
    To piggyback off JosefZ's comment, you seemed confused on why it's staying in the same window when you're specfying /B. If you're looking to execute it as "invisible", the PowerShell.exe cli allows for an option of hidden. So there's no need to call cmd.exe again when you can use PowerShell.exe -WindowStyle hidden. Commented Sep 12, 2022 at 20:36
  • 1
    Yes, that works perfectly. Sorry I didn't know how to explain it properly. Thank you Abraham Zinala Commented Sep 12, 2022 at 20:40

1 Answer 1

1

To build on the helpful comments and your own answer, using a simplified command that waits 2 seconds and then launches Notepad:

powershell -WindowStyle Hidden "Start-Sleep 2; Start-Process Notepad.exe" & exit
  • Given that you want to close the current window and launch the PowerShell command invisibly, there is no need for using cmd.exe's internal start command.

    • As an aside:
      • As pointed out, start /B by design launches the given executable in the current window.

      • If you were to omit /B but still use start, powershell.exe would invariably launch visibly first, and -WindowStyle Hidden would only take effect afterwards, resulting in a brief flashing of the window - this is a long-standing problem that may get fixed in a future edition of PowerShell (Core), the modern, cross-platform successor to Windows PowerShell - see GitHub issue #3028.

  • The above invokes powershell.exe synchronously, which means that it will run in the current cmd.exe window, but - due to -WindowStyle Hidden - then hides the current window.

    • That is, the current window is virtually instantly hidden, but the cmd.exe session lives on; once the powershell.exe call has completed, however, & exit ensures that the cmd.exe session too is exited.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.