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.

31 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?


  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.

      1. Hi,
        thanks for this, we’ve had a couple of issues servicing our Windows 10 WIM in SCCM, so I wrote some powershell to mount and update it,… I’ve now added multiselect so we can add more than one update at a time.


  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?

  5. Unfortunately, open Dialog box script doesn’ work for me. I copied and pasted the 2nd last scrip (which I think is the full script) to PowerShell ISE but it doesn’t show anything.


    1. Hi Tariq,

      If you read the post more closely, you’ll see that you have to do more than run the second to last script. That declares a function that you need to actually call aftewards.

  6. This script is amazing! The only thing I am running into issues with is that the dialog box opens behind powershell. Is there anyway to have the dialog box open to the front of the screen?


    1. Interesting, I haven’t had that issue myself. Does it happen with the same script on other machines?

      1. It only happens when I am using VS Code for PowerShell. If I use the Powershell ISE then it pops right up as expected. To work around this, I decided to add a section to my script to minimize VS Code when the dialog box opens. Or I will instruct my co-workers to use the ISE :).

        1. They should be running the finished scripts in PowerShell, as in the console 🙂

          ISE and VSCode are not PowerShell, even though they include PowerShell console panes. There’s a lot that makes them different. To see how your finished script or module runs, it should be tested in the normal console, which is where end users should run them, too.

  7. This is absolutely awesome Thomas. I am having a hard time working with the filename after selecting it. What I am trying to do is reference the filename with an outlook I have.

    What do I need to call correctly to get this to work for this line? — $msg = $ol.CreateItemFromTemplate($OpenFileDialog.filename)


    Function Get-FileName($initialDirectory)
    [System.Reflection.Assembly]::LoadWithPartialName(“”) | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = “MSG (*.msg)| *.msg|EML (*.eml)| *.eml”
    $OpenFileDialog.ShowDialog() | Out-Null

    $ol = New-Object -ComObject Outlook.Application
    $msg = $ol.CreateItemFromTemplate($OpenFileDialog.filename)
    $headers = $msg.PropertyAccessor.GetProperty(“”)
    $headers | Out-File -filepath “C:\Users\andrrrpilll\Desktop\Resources2\IR\Emails\2StringSA.txt”
    get-content “C:\Users\andrpil\Desktop\Resources2\IR\Emails\2StringSA.txt” | Select-String -pattern ‘date’, ‘subject’, ‘X-Originating-IP’, ‘X-sender’, ‘Reply-To’, ‘Return-Path’, ‘To:’, ‘From:’, ‘Received: from’


    1. Hey Drew, unfortunately, I’m not familiar with the Outlook com objects enough to tell you how to work with them.

      1. No worries! Actually what I posted worked! Learned with what I have above required outlook to be closed since you are calling an outlook object.

        I appreciate your post!

  8. Can we change the name at the top of the file open dialog? Instead of “open” it could say “select a zip file” or something?

    1. You sure can. Just edit the Title attribute on $OpenFileDialog. So something like $OpenFileDialog.Title = “select a zip file”.

Leave a Reply

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