In this post, I’ll show a function I created for selecting a folder from your local machine. This function will return the path to a selected folder.
FUNCTION Definition
The following is the function wrapper which will contain the remainder of the code.
FUNCTION Show-FolderBrowserDialog { <# .Synopsis Opens the Folder Browser dialog and allows you to select a local folder. .DESCRIPTION Opens the Folder Browser dialog and allows you to select a local folder. You can specify the root folder and make the new folder button available.
.EXAMPLE Opens the folder browser dialog without any options.
Opens in the MyDocuments folder by default. And the new folder button is not available.
$folder = Show-FolderBrowserDialog -Verbose
.EXAMPLE
Opens the folder browser dialog with options.
Opens in the MyDocuments folder by default. And the new folder button is available.
$folder = Show-FolderBrowserDialog -ShowNewFolderButton -Verbose
.EXAMPLE
Opens the folder browser dialog with options.
Opens in the specified folder. And the new folder button is available.
$folder = Show-FolderBrowserDialog -RootFolder = 'c:\temp' -ShowNewFolderButton -Verbose #> [CmdletBinding()] param (...) # end param... BEGIN {...} # end BEGIN... PROCESS {...} # end PROCESS... END{...} # end END... } # end FUNCTION Show-FolderBrowserDialog... Export-ModuleMember -Function Show-FolderBrowserDialog
Parameters
First thing we need to do is create our parameter and value pairs. The help messages will explain the purpose for each parameter.
param (
# Path of folder to open in the dialog. By default it opens in the MyDocuments folder.
[ValidateNotNullOrEmpty()]
[Parameter()]
[string] $RootFolder = [System.Environment+SpecialFolder]::MyDocuments,
# When included, the New Folder button will be enabled.
[Parameter()]
[switch] $ShowNewFolderButton
) # end param...
BEGIN
I want to take a second and point out a couple of conventions that I use in my scripts. First, you may notice that I put a comment at the end of many of the blocks of code. Doing this makes it easier for me to determine where a block of code starts and ends. Comes in handy when troubleshooting a script. I also add text ([BEGIN], [PROCESS], [END]) in the output messages (Write-Verbose, Write-Warning, Write-Error) corresponding to the section of the script I am in. Again, this makes it obvious where something occurs within the script.
BEGIN {
$CmdletName = $MyInvocation.MyCommand.Name
$StartTime = Get-Date
Write-Verbose -Message "[BEGIN] $StartTime - Entering '$CmdletName' Function..."
Add-Type -AssemblyName System.Windows.Forms
} # end BEGIN...
PROCESS
Now, we will proceed to the PROCESS code block.
Here we will show the folder browser dialog and return the path to the selected folder.
PROCESS {
try {
# Create the FolderBrowserDialog object.
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
Description = "Select desired folder."
SelectedPath = $RootFolder
ShowNewFolderButton = $ShowNewFolderButton
}
# The "(New-Object System.Windows.Forms.Form -Property @{TopMost = $true; TopLevel = $true}" is required to ensure the dialog opens on top of all other applications. Otherwise, it will open behind.
$result = $FolderBrowserDialog.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true; TopLevel = $true}))
# Only returns a folder path when the "OK" button is clicked.
if($result -eq 'OK'){
# Output the name for the selected folder.
Write-Verbose -Message "'$($FolderBrowserDialog.SelectedPath)' was selected."
Write-Output $FolderBrowserDialog.SelectedPath
} else {
throw "Folder browser dialog was canceled."
}
} catch {
Write-Warning -Message "[PROCESS] $_"
} # end try/catch...
} # end PROCESS...
END
Lastly, we will proceed to the END block of code.
END {
# Release all memory for the dialog.
$FolderBrowserDialog.Dispose()
$EndTime = Get-Date
Write-Verbose -Message "[END] It took $($EndTime - $StartTime) to complete the process."
Write-Verbose -Message "[END] $EndTime - Exiting '$CmdletName' Function..."
} # end END...
EXAMPLE
In the following example I will show the FolderBrowser Dialog with the rootfolder parameter set to “c:\temp” and will include the ShowNewFolderButton parameter.
$folder = Show-FolderBrowserDialog -RootFolder 'c:\temp' -ShowNewFolderButton -Verbose
The following shows the folder browser dialog. I will need to scroll down to the “c:\temp” folder.

Create new “Reports” folder
Selected the “PWTools” folder within the “c:\temp” folder.
Clicked the Make New Folder button
Entered “Reports” for the name.
Clicked OK.

The “C:\TEMP\PWTools\Reports” folder path will be returned.
The following shows the results within PowerShell.

Experiment with it and have fun. Hopefully, you find this useful.
The following is a link to a zip file containing the script.
FUNCTION Show-FolderBrowserDialog
Please let me know if you have any questions or comments. If you like this post, please click the Like button at the bottom of the page.
