Share via


Open File Dialog Window PowerShell don't appear


Open File Dialog Window PowerShell don't appear


Summary

This TechNet Wiki is based on the TechNet Forum Post.
Powershell: Script won't open explorer window to browse for .csv file

Issue

The Open File Dialog box is not appearing when we execute the below code

Add-Type -AssemblyName System.Windows.Forms
$fd = New-Object system.windows.forms.openfiledialog
$fd.InitialDirectory = 'C:\Temp'
$fd.MultiSelect = $false
$fd.Filter = '(*.csv) | *.csv'
$fd.showdialog()

No errors but does nothing.

Solution

This is not a code issue but a threading issue. The dialog box will not appear in MTA mode. In PSV2 the PowerShell Console is MTA by default. The same code works fine in ISE because it's STA.

How to check the threading mode?
[System.Threading.Thread]::CurrentThread.ApartmentState

If this returns MTA the above code will not work. In latest version of PowerShell this works fine as expected.

Fix

Open PowerShell Console and run the below code

powershell.exe -sta -File C:\Code.PS1