Powershell Open File Dialog Box

Do you ever require the users of your script to select which file to use?

I do. And it’s no fun trying to do decent error-handling when a user has to type the full path to a file. Nor is it any fun to be that user. So why not use the Windows Forms class of .Net to show the Open File Dialog Box?

Select-FileDialog function (rename to .ps1 or copy into your profile)

User happy. Me happy. You happy?

16 thoughts on “Powershell Open File Dialog Box

  1. I’m happy 🙂
    Very good example on how to use winforms.

    I referenced you on my blog for getting started with PS/toolkit.

    Again thanks for a nice blog!

  2. The function hangs at this point.
    $Show = $objForm.ShowDialog()
    But the Dialogbox does not appear
    I think i’m missing samething.

  3. There seems to be a problem with powershell hanging when using the showdialog method of the openfiledialog object… There seems to be a solution to this however which is to set the ShowHelp property to $true. The default is $false, and for some reason if it is set to $false powershell will hang.

  4. Thanks for posting this. Very helpful! Just had to make the correction that Jamey pointed out and it worked great!

  5. Thanks, worked for me too, appears to hang, but check the file/dialogue box hasnt appeared behind something else on screen! Anyone know how to alter this to allow selection of file Paths rather than specific files?

  6. Quite useful, though when I use it with the PowerShell ISE, it puts the file dialog window behind the ISE.

    Here’s a tip: You can have multiple file catagories using arguments similar to:
    -Filter “Text files (*.txt)|*.txt|All files (*.*)|*.*”

  7. multiple file category arguments should not include extra space:
    -Filter “Text files (*.txt) | *.txt | All files (*.*) | *.*”
    does not work — only show *.* filter.
    -Filter “Text files (*.txt)|*.txt|All files (*.*)|*.*”

Leave a Reply