2

I have the following code which should allow a user to pick a folder location for a specific file and then I will use that location to take action on the file. However, when I run the script outside of the ISE the FolderBrowserDialog doesn't appear at all. I have looked behind the powershell window and behind all other windows and it never appears.

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = "Description"
$FolderBrowser.ShowNewFolderButton = $false
$FolderBrowser.RootFolder = 'MyDocuments' 
if($FolderBrowser.ShowDialog() -eq $true){
     Do some stuff
}

Any thoughts on why it might not show up? Are there certain powershell settings that need to be turned on to have things appear from the console?

Sam
  • 63
  • 6

1 Answers1

3

I've had this issue before in PS v2, here's how I have fixed it.

Save the code in a .ps1 file and run it like this:

powershell -STA -File "path/to/file.ps1"

ISE v2 runs in STA, and console v2 runs in MTA.

More information: Could you explain STA and MTA?

Community
  • 1
  • 1
sodawillow
  • 12,497
  • 4
  • 34
  • 44