Use Open File Dialog

by Feb 5, 2015

All PowerShell versions

To add some sophistication to your script, here is a simple function that opens the OpenFile dialog and lets the user pick a file.

function Show-OpenFileDialog
{
  param
  ($Title = 'Pick a File', $Filter = 'All|*.*|PowerShell|*.ps1')
  
  $type = 'Microsoft.Win32.OpenFileDialog'
  
  
  $dialog = New-Object -TypeName $type 
  $dialog.Title = $Title
  $dialog.Filter = $Filter
  if ($dialog.ShowDialog() -eq $true)
  {
    $dialog.FileName
  }
  else
  {
    Write-Warning 'Cancelled'
  }
}

As you can see, you control the dialog title and also the file types displayed.

Twitter This Tip! ReTweet this Tip!