Test for STA mode

by Jun 24, 2011

By default, the PowerShell console does not use the STA mode whereas the ISE editor does. STA is needed to run Windows Presentation Foundation scripts and to use WPF-based dialog windows, such as Open.

Here is how you can check to see whether your script is running in a host that uses STA mode:

function Test-STA {
  $Host.Runspace.ApartmentState -eq 'STA'
}

 

Twitter This Tip!
ReTweet this Tip!