Opening PowerShell Script and hide Command Prompt, but not the GUI

后端 未结 4 2139
深忆病人
深忆病人 2020-12-11 06:54

I currently open my PowerShell script through a .bat file. The script has a GUI. I tried to put this in my script but it hid the GUI as well and also kept looping because I

4条回答
  •  失恋的感觉
    2020-12-11 07:29

    If you run PowerShell from a shortcut with the window set to minimized, it will briefly flash a cmd icon in the taskbar but you barely notice it. Yet, it will start your PowerShell GUI without having a console window behind it.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle Hidden -file "C:\path\whatever.ps1"
    

    If you want to start a second GUI console window from whatever.ps1 without it stopping the processing on whatever.ps1 you need to use start-process. However, start-process with -WindowStyle hidden prevents the GUI from showing up. Removing -WindowStyle shows a command window behind your GUI. However, if you start-process with cmd.exe /k, it does work.

    $argumentlist = "/c powershell.exe -file `"c:\path\whatever2.ps1`" -param1 `"paramstring`""
    Start-Process cmd.exe -WindowStyle Hidden -ArgumentList $argumentlist
    

    As a bonus, if you start whatever2.ps1 with a param() statement, you can pass named, required arguments. Just be sure it's the very first thing in the ps1 file, before assemblies even.

    param (
      [Parameter(Mandatory=$true)]
      [string]$var1
    )
    $argumentlist = "/c powershell.exe -file `"C:\path\whatever2.ps1`" -param1 `"param1string`""
    

提交回复
热议问题