PowerShell: folderbrowserdialog problems

oynaz

Platinum Member
May 14, 2003
2,449
2
81
Hi guys,

Newbie here.
I am trying to make a dialog which enables the user to select a folder, which will be used later in the script as a source for copying some files.

When I start the script, nothing happens, and the PowerShell command shell windows hangs.
What am I doing wrong?

Code:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

#This function asks for the location of the build folder
function buildfolderloc {
	$foldername = new-object System.Windows.Forms.folderbrowserdialog
	$foldername.showdialog()
	$script:installfoldername = $foldername.SelectedPath
}

buildfolderloc

Write-Host $installfoldername
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
The FolderBrowserDialog doesn't work in PowerShell v1.0 because of its threading model. I heard v2.0 fixes it, but haven't tried it.

I saw somebody who recommended using the Shell.Application com object for a workaround:

$app = new-object -com Shell.Application
$folder = $app.BrowseForFolder(0, "Select Folder", 0, "C:\")
if ($folder.Self.Path -ne "") {write-host "You selected " $folder.Self.Path}
 

oynaz

Platinum Member
May 14, 2003
2,449
2
81
I am using PowerShell 2, and I am sure I have seen it work before. Strange.

Your workaround works for me, though. Thank you.