PowerShell: folderbrowserdialog problems

Discussion in 'Programming' started by oynaz, Apr 17, 2013.

  1. oynaz

    oynaz Platinum Member

    Joined:
    May 14, 2003
    Messages:
    2,446
    Likes Received:
    2
    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
    
     
  2. Loading...

    Similar Threads - PowerShell folderbrowserdialog problems Forum Date
    matlab for solve the problem by using NR Programming Dec 18, 2017
    PowerShell Quetion. Programming Nov 14, 2017
    Preparing for the year 2038 problem Programming Jun 26, 2017
    Python or powershell for automation? Programming Jan 20, 2017
    Renaming files in aggregate using Powershell Programming Nov 27, 2016

  3. KB

    KB Diamond Member

    Joined:
    Nov 8, 1999
    Messages:
    4,898
    Likes Received:
    78
    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}
     
  4. oynaz

    oynaz Platinum Member

    Joined:
    May 14, 2003
    Messages:
    2,446
    Likes Received:
    2
    I am using PowerShell 2, and I am sure I have seen it work before. Strange.

    Your workaround works for me, though. Thank you.
     
  5. deadlydog

    deadlydog Junior Member

    Joined:
    May 17, 2013
    Messages:
    1
    Likes Received:
    0