Create a Folder Selector

by Dec 9, 2013

To add a little glamour to your scripts, here are a few lines of code that display a folder selector dialog. When a user selects a folder, your script receives the selection and can work with the selected path:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$null = $FolderBrowser.ShowDialog()
$Path = $FolderBrowser.SelectedPath

"You selected: $Path" 

Note the first two lines: they are not necessary when you run the code inside the ISE editor, but you do need them inside the powershell.exe. So keep these lines to make sure your code runs in all PowerShell hosts.

Twitter This Tip! ReTweet this Tip!