Open File Dialog Box In PowerShell

Here’s a neat little PowerShell function you can throw into your scripts. Lots of times I want to specify a CSV or TXT or some other file in a script. It’s easy to do this:

But that means you have to type the whole absolute or relative path to the file. What a pain. I know what you’re thinking… There must be a better way!

There is! Use an open file dialog box. You know, like when you click File, Open and a window opens and you navigate your filesystem and select a file using a GUI. How do you do it in PowerShell? Let me show you. First things first: let’s declare a function with a couple of the items we’re going to need.

I’m going to name this function Get-FileName because I like the Verb-Noun naming scheme that PowerShell follows. It’s got a parameter, too. $initialDirectory is the directory that our dialog box is going to display when we first launch it. The part of this that most likely looks new is line 3. We need to load a .NET item so we can use the Windows Forms controls. We’re loading via partial name because we want all the Windows Form controls, not just some. It’s faster and easier to do this than it is to pick and choose. We’re piping the output to Out-Null because we don’t want all the verbose feedback it gives when it works.

Now let’s open the thing and get to business selecting a file.

On line 5, we’re creating a new object. That object is unsurprisingly an OpenFileDialog object. On line 6 we’re specifying that initial directory that we got in the parameter. On line 7 we’re doing something a little interesting. The filter attribute of the OpenFileDialog object controls which files we see as we’re browsing. That’s this part of the box.

I’m limiting my files to CSV only. The first part of the value is CSV (.csv) which is what the dialog box shows in the menu. The second part after the pipe character *.csv is the actual filter. You could make any kind of filter you want. For instance, if you wanted to only see files that started with “SecretTomFile”, you could have a filter like SecretTomFile.

The next item on line 8 is to open the dialog box, we do that with the ShowDialog() function. We discard the output from this command because it’s spammy in this context, just like when we added the .NET items.

One last thing! We’ve created, defined and opened our OpenFileDialog box but don’t we actually need to get the result of what file was selected? Yes, we do. That’s pretty easy, though.

The Filename attribute is set when someone commits to opening a file in the OpenFileDialog box. On line 9, we’re returning it to whatever called our script.

So to use this function in the same way as the example at the top of this post, your code would look like this.

I think this is a lot nicer than typing a filename every time you want to run a script. I find it particularly convenient on scripts I run a lot.

0 thoughts on “Open File Dialog Box In PowerShell

  1. If I waqnted to have this function available to any script I write in the future would I put it right in my profile? Or what would be the best way to make this function avaialble to all the scripts I write without haqving to rewrite it into every script?

    1. If you’re the only one writing the script, then yes, you’d be safe to put it in your profile. If you’re sharing your scripts, the best way to make sure everyone has the function available to them is to just include it with each script, or make sure they have it in their profiles too.

  2. Hi, I’m a n00bie to this, so please excuse my silly question. I am able to use this function une running the script from the “PowerGUI Script Editor”, however at the PowerShell, command prompt I simply get a blank screen, and nothing happens. The dialog window for explorer never shows up. Is there anything I need to set in my environment?

    TIA
    -danzenie

  3. I like this however I needed to open two files and store them in two variables to be used in my script but the script just hangs there and when it works it’s very slow, Any suggestion? Thanks

    1. If you add the line $OpenFileDialog.Multiselect = $true before you trigger the box to appear, and specify “filenames” instead of “filename” in the last line of the function, you’ll be able to ctrl+click multiple files and have them returned as an array.

  4. Sorry to hop into an older thread, but hoping someone can help. Is there any way to set the OpenFileDialog to display HIDDEN files?

Leave a Reply

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