The open file/folder dialog box is a great way to receive input for your scripts interactively. It provides a file browser that makes for a much more user-friendly approach than merely prompting for a path. In this post I show you how can use OpenFileDialog in your PowerShell scripts.

Adam Bertram

Adam Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at, or follow TechSnips on Twitter at @techsnips_io.

When you're using a Windows application and need to provide input for a file or folder, you've probably seen the standard open file dialog.

Open file dialog

Open file dialog

This dialog box is standard across lots of Windows applications. The software you're using to invoke this dialog box uses a .NET assembly called System.Windows.Forms with a class inside called OpenFileDialog. Did you know you can get input to your PowerShell scripts this way too? Since PowerShell lies directly on top of .NET, we can invoke any .NET class we need, which means we can also bring up the open file dialog box.

To do this, we'll first need to load the System.Windows.Forms assembly manually using the Add-Type cmdlet. Lots of .NET assemblies are typically loaded for you, but in this case, we have to do it manually.

Once we've loaded the assembly, we can instantiate an OpenFileDialog object using New-Object.

You can see above that the OpenFileDialog class constructor has an InitialDirectory argument. This tells the OpenFileDialog class which folder to display when the dialog box comes up. In this case, I have the dialog box to display the desktop.

At this point, the dialog box will not display. We're just instantiating the object. To show the dialog box, we'll have to use the ShowDialog() method.

This will display the dialog box. I'm assigning the output of ShowDialog() to $null. This is because the output does not return anything useful for our purposes. You might expect the output to return the chosen file name, but it doesn't. The system then stores the file information in the OpenFileDialog object itself.

You can see above that the OpenFileDialog object now contains all the information gathered from the file chosen.

The above example allows me to choose any file we'd like, but we also can limit the input by file type too using the Filter property.

Now when the dialog box displays, you can see below that the only options are to choose between Word and Excel documents.

Filtering by file

Filtering by file

For more information about this method of receiving file location input from users, refer to the Microsoft MSDN information.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Users who have LIKED this post:

  • avatar
  • avatar
  • avatar
  1. Jimmy 1 year ago

    Nice article 🙂


  2. matt gallagher 1 year ago

    Thanks for an insight into some of the .net classes that PS can utilise (coming from a non dev. background) - keep it coming!


  3. Allan Weaver 1 year ago

    Good article, but I can't see the filtered file display at the bottom.



  4. Shawn 2 months ago

    Is it possible to add a msg? Like: "Please choose the required file..."


    • Leos Marek 2 months ago

      Yes, by using the Title property, example below:



  5. Sondre Sætervadet 1 month ago


    The file i select when i can choose a file wont open when i press open


  6. David Figueroa 1 month ago

    Sondre - the dialog is just going to give you the name of the file, and that's it.  It doesn't *do* anything with it, that is up to you and your code. Your code needs to take the file and do whatever with it.  

    If you use a ShellExecute method or anything that will call the default verbs, it will run whatever program is associated with that file type, using the command string defined in the file association passing the file name to that string.  So.. if you pick a .docx, and use the default exec, it will launch Word and feed in the file you picked.  If there isn't a file association for that type, you will get the Windows prompt to pick a program to run for that file.

    David F. 


  7. dunca 3 weeks ago

    thanks man very very nice article, and good job 


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2019


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account