• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PowerShell: folderbrowserdialog problems

oynaz

Platinum Member
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
 
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}
 
I am using PowerShell 2, and I am sure I have seen it work before. Strange.

Your workaround works for me, though. Thank you.
 
Back
Top