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?

  • Share/Bookmark

No related posts.

  • rsj

    October 31st, 2008

    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!

  • Harald Scholl

    December 12th, 2008

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

  • Jamey Westmoreland

    July 17th, 2009

    I think there is a mistake in your Example use for the filter. You have
    -Filter “Powershell Scripts|(*.ps1)”

    I think is should be
    -Filter “Powershell Scripts (*.ps1)|*.ps1″
    I made that change and then was able to see files

  • antize

    September 3rd, 2009

    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.

Leave a Comment

* are Required fields