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?

Tagged with:

13 Responses to Powershell Open File Dialog Box

  1. rsj says:

    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. Harald Scholl says:

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

  3. Jamey Westmoreland says:

    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


  4. antize says:

    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.

  5. Jonathon says:

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

  6. Andy says:

    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?

  7. RJ says:

    Jamey Westmoreland is right. I had to modify the -Filter to see the desired files

  8. Exchange 2007 – Tracking and Analysing Large Mailbox Move Reports – using the ExchangeMoveMailboxTaskViewer Tool… « telnet 25 says:

    […] used to display a Windows open dialog box – this was based on code borrowed from Hugo Peeters – whom I send my thanks to for writing an excellent […]

  9. […] Standaard denk ik dat er zoiets niet in powershell zit. Maar even googlen en ik kwam uit op deze link: Powershell Open File Dialog Box | PeetersOnline […]

  10. AnthonyA says:

    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 (*.*)|*.*”

  11. Abhishek says:

    i am happy!

  12. Luke says:

    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