Detecting STA-Mode

by Nov 9, 2012

Here is a simple line that tells you whether PowerShell runs in MTA- or STA-mode:

[Runspace]::DefaultRunspace.ApartmentState -eq 'STA'

This information may be important because only in STA-mode can PowerShell run WPF functionality (like windows based on WPF or system dialogs like Open or Save As). In PowerShell 3.0, both console and ISE run in STA mode by default. In PowerShell 2.0, the console used MTA mode. It needs to be started with the parameter -STA to enable STA-mode.

Twitter This Tip! ReTweet this Tip!