Get folder path from explorer menu to powershell variable

Is it possible to open the explorer window from powershell and save the path selected in explorer to a variable?

to open explorer window from powershell

PS C:> explorer

+4
source share
4 answers

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 
+10
source

I found using reflection in the selected answer a bit uncomfortable. 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 paste the appropriate code:

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

Above did not work for me. Starting Windows 7 with Powershell version 2. I found the following that allowed a popup 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 problems.

+1
source

I just wanted to publish an addendum, I believe that between the passage | missing intermediate:

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

and:

 Out-Null 
0
source

All Articles