Skip to content

Instantly share code, notes, and snippets.

@F1XI
Created July 25, 2022 07:46
Show Gist options
  • Save F1XI/f9a66f36e4a3bb1773aafe93bd867852 to your computer and use it in GitHub Desktop.
Save F1XI/f9a66f36e4a3bb1773aafe93bd867852 to your computer and use it in GitHub Desktop.
Usage of FolderBrowserDialog in Powershell
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
function Get-Folder() {
param
(
[Parameter(Mandatory=$false)][string]$initialDirectory = $ENV:USERPROFILE
)
try {
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.SelectedPath = $initialDirectory
$FolderBrowserDialog.Description = 'Select a folder'
$FolderBrowserDialog.ShowNewFolderButton = $false
if ($FolderBrowserDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
return $FolderBrowserDialog.SelectedPath
}
}
catch {
Write-Host $_
Write-Host $_.Exception
Write-Host $_.ScriptStackTrace
exit 1
}
return $null
}
$SelectedPath = Get-Folder
if ($SelectedPath -ne $null)
{
$SelectedPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment