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
}
If ($IsSTAEnabled -eq $false) {
“You need to run this script with -STA switch or inside ISE”
Exit 1
}