powershell browse to folder code example

Example: browse for folder powershell

function BrowseForFolder($initialDirectory="")
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
    $folderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $folderBrowserDialog.Description = "Select a folder"
    $folderBrowserDialog.rootfolder = "MyComputer"
    $folderBrowserDialog.SelectedPath = $initialDirectory
    
   if ($folderBrowserDialog.ShowDialog() -eq "OK")
   {
      return $folderBrowserDialog.SelectedPath
   } else {
      return $initialDirectory
   }
}


$window.FindName("TargetDir").Text = $outDir

# Browse for target dir
$BrowseForTargetDirButton = $window.FindName("BrowseForTargetDir")
$BrowseForTargetDirButton.Add_Click({
   $currentDir = $window.FindName("TargetDir").Text
   $window.FindName("TargetDir").Text = BrowseForFolder($currentDir)
})