1
Function Get-Folder($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "NetworkShortcuts"

    if($foldername.ShowDialog() -eq "OK")
    {
        $folder += $foldername.SelectedPath
    }
    return $folder
}

$a = Get-Folder

How do I specify the initial directory instead of Desktop or NetworkShortcuts. I want to specify the path like F:\Folder1

software is fun
  • 7,286
  • 18
  • 71
  • 129

2 Answers2

1

This is the function I keep on hand in case I ever need it:

Function Get-FolderPath{
[CmdletBinding()]
Param(
    [String]$Description,
    [String]$InitialDirectory = "C:\",
    [Switch]$NewFolderButton = $false)

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
    $FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $FolderBrowserDialog.SelectedPath = $InitialDirectory
    $FolderBrowserDialog.Description = $Description
    $FolderBrowserDialog.ShowNewFolderButton = $NewFolderButton
    If($FolderBrowserDialog.ShowDialog() -eq "OK"){
        $FolderBrowserDialog.SelectedPath
    }
}

Usage is pretty simple:

$a = Get-FolderPath -Description "Select a folder" -InitialDirectory "F:\Folder1" -NewFolderButton

Or omit the -NewFolderButton if you don't want that button to show up.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
1

Swap your rootfolder for SelectedPath and that should be what you're looking for.

Function Get-Folder($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.SelectedPath = "F:\Folder1"

    if($foldername.ShowDialog() -eq "OK")
    {
        $folder += $foldername.SelectedPath
    }
    return $folder
}

$a = Get-Folder
Booga Roo
  • 1,665
  • 1
  • 21
  • 30