Browser window causing script to lock up when Powershell started via *.bat file

Viewed 52

I have a Powershell script that calls a browser window to select a file. When I run the script in ISE it works correctly. However, when I call it from a batch file opening the browser file causes the script to go non-responsive. The code I am calling the file with is this.

Powershell.exe –ExecutionPolicy Bypass –File “<pile path>”

The code for the Brows call is this.

$fileBrows = New-Object System.Windows.Forms.OpenFileDialog
$null = $fileBrows.ShowDialog()
Return $fileBrows.FileName

I have run the as normal and as Administrator, but this make no difference.

Any ideas?

2 Answers

Include Add-Type -assembly System.Windows.Forms at the top of your script and try again.

The script should look like this

Add-Type -assembly System.Windows.Forms
$fileBrows = New-Object System.Windows.Forms.OpenFileDialog
$null = $fileBrows.ShowDialog()
Return $fileBrows.FileName

Adding Add-Type -assembly System.Windows.Forms as the example above worked.

Related