Search code examples
wpfxamlpowershellpowershell-2.0sta

Change powershell mode to STA in script


I have a powershellscript which displays a WPF form using XAML. To execute this script, the powershell console have to be launched in STA mode (PS 2.0 default in MTA mode).

I know the opportunity, to change the mode in the console with this command:

powershell -sta

But in future my script will be executed automatically by an other programm without any parameter. So the console can't be called first with the -sta parameter.

Is there any opportunity to change the mode directly in the powershell script, befor the WPF/XAML is launched?


Solution

  • Here an simple example:

    $x = {
        for($i= 0;$i -le 10;$i++){
            Write-host $i
        }
    }
    
    Start-Process Powershell.exe -argumentlist "-sta -NoExit -NoProfile -ExecutionPolicy Bypass -Command $X"