get a folder path from the explorer menu to a powershell variable

naveejr picture naveejr · Jul 10, 2012 · Viewed 25.5k times · Source

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

Answer

CB. picture CB. · Jul 10, 2012

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