5

is it possible to open a explorer window from powershell and store the path selected in the explorer, to a variable?

to open explorer window from powershell

PS C:> explorer

6 Answers 6

13

Maybe this script is what you want:

Function Select-FolderDialog
{
    param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
     Out-Null     

   $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
        $objForm.Rootfolder = $RootFolder
        $objForm.Description = $Description
        $Show = $objForm.ShowDialog()
        If ($Show -eq "OK")
        {
            Return $objForm.SelectedPath
        }
        Else
        {
            Write-Error "Operation cancelled by user."
        }
    }

Use as:

$folder = Select-FolderDialog # the variable contains user folder selection
Sign up to request clarification or add additional context in comments.

1 Comment

You need to move the assembly loading after the param block.
4

I found the use of reflection in the selected answer to be a little awkward. The link below offers a more direct approach

http://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/

Copy and pasted relevant code:

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath

Comments

1

The above did not work for me. Running Windows 7 with Powershell Version 2. I did find the following, which did allow the pop-up and selection:

    Function Select-FolderDialog
    {
         param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

     [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") Out-Null     

     $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
     $objForm.Rootfolder = $RootFolder
     $objForm.Description = $Description
     $Show = $objForm.ShowDialog()
     If ($Show -eq "OK")
     {
         Return $objForm.SelectedPath
     }
     Else
     {
        Write-Error "Operation cancelled by user."
     }
    }

Just in case others have the same issues.

1 Comment

Works but has the issue of not showing the dialog in front sometimes.
0

Just wanted to post an addendum, I believe there is a pipe | missing from in-between:

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

and:

Out-Null

1 Comment

Missing in which answer / piece of code? As far as I can see, there is a pipe at least in the answer that's been accepted.
0

Here is a solution that opens explorer dialog window, asking user to select a folder. Then stores the folder path inside a variable named "path":

Add-Type -AssemblyName System.Windows.Forms
$browser = New-Object System.Windows.Forms.FolderBrowserDialog
$null = $browser.ShowDialog()
$path = $browser.SelectedPathode

Comments

0

Your code worked for me only after I have replaced ".SelectedPathode" with ".SelectedPath"

Add-Type -AssemblyName System.Windows.Forms
$browser = New-Object System.Windows.Forms.FolderBrowserDialog
$null = $browser.ShowDialog()
$path = $browser.SelectedPath

Here is also the documentation regarding the respective property - https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog.selectedpath?view=net-5.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.