Checking -STA Mode

by Jan 17, 2011

PowerShell needs to run in STA mode to display Windows Presentation Foundation (WPF) windows. ISE runs in STA mode by default whereas the console will need to be launched explicitly with the -STA switch. To check whether STA mode is currently enabled, you can use this line in your scripts:

$IsSTAEnabled = $host.Runspace.ApartmentState -eq ‘STA’
If ($IsSTAEnabled -eq $false) {
“You need to run this script with -STA switch or inside ISE”
Exit 1
}

Twitter This Tip!
ReTweet this Tip!