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.

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.

Add-Type -AssemblyName System.Windows.Forms

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

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = [Environment]::GetFolderPath('Desktop') }

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.

$null = $FileBrowser.ShowDialog()

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.

PS C:\> $FileBrowser

CheckFileExists              : True
Multiselect                  : False
ReadOnlyChecked              : False
ShowReadOnly                 : False
SafeFileName                 : Thumbs.db
SafeFileNames                : {Thumbs.db}
AddExtension                 : True
CheckPathExists              : True
DefaultExt                   :
DereferenceLinks             : True
FileName                     : \\Mac\Home\Desktop\Thumbs.db
FileNames                    : {\\Mac\Home\Desktop\Thumbs.db}
Filter                       :
FilterIndex                  : 0
InitialDirectory             : \\Mac\Home\Desktop
RestoreDirectory             : False
ShowHelp                     : False
SupportMultiDottedExtensions : False
Title                        :
ValidateNames                : True
CustomPlaces                 : {}
AutoUpgradeEnabled           : True
Tag                          :
Site                         :
Container                    :

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.

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
    Filter = 'Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx'
$null = $FileBrowser.ShowDialog()

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

Subscribe to 4sysops newsletter!

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.

  1. Avatar
    Jimmy 5 years ago

    Nice article 🙂

  2. Avatar
    matt gallagher 5 years 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. Avatar
    Allan Weaver 5 years ago

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


  4. Avatar
    Shawn 4 years ago

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

    • Avatar
      Leos Marek (Rank 4) 4 years ago

      Yes, by using the Title property, example below:

      $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
          InitialDirectory = [Environment]::GetFolderPath('Desktop') 
          Filter = 'Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx'
          Title = 'Select files to open'


  5. Avatar
    Sondre Sætervadet 4 years ago


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

    • Avatar
      Nevor Yard 2 years ago

      Try this:

      start $FileBrowser.filename

  6. Avatar

    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. 

    • Avatar
      NameInUse 2 years ago

      So how to use ShellExecute in this case to open the selected file??

  7. Avatar
    dunca 4 years ago

    thanks man very very nice article, and good job 

  8. Avatar
    Stevie 4 years ago

    Great article – this just enhanced a script I wrote 10mins ago.

    I'm immediately looking to see if there is a CreateFileDialog object type too – excellent for those scripts which need output, and we don't want to hard-code paths if we can avoid it.

  9. Avatar
    Aniket 4 years ago

    Very good blog and helpful. Similarly how can we select folder path instead of file?? 

    Thanks in Advance 🙂


  10. Avatar

    You just need to use the correct .Net class..

    Add-Type -AssemblyName System.Windows.Forms
    $dialog = [System.Windows.Forms.FolderBrowserDialog]::new()
    $dialog.Description = 'This is a description'
    $dialog.RootFolder = [System.Environment+specialfolder]::Desktop
    $dialog.ShowNewFolderButton = $true

    The documentation is at 

    David F. 

    • Avatar
      Mir 2 years ago

      Hi David
      I was looking for this for longtime. But I have one more questions. I am very new in this scripting world but learning… I am trying to create a script that will install printer package onto remote machine. when I will run the script it will open the dialog box to select the printer package. I got it so far to select now how can I parse that to copy the selected file to the remote machine. I know the copy part but wondering how to use this dialog box part to select in the copy portion. I would appreciate your answer if you can. Thanks a lot in advance.

      • Avatar

        The return of the dialog should be the folder path. If you want the file, you need the FileBrowserDialog instead (almost exactly the same and it returns the full file path). Does that answer the question?

        David F.

  11. Avatar
    Aniket Sonkusare 4 years ago

    Thanks David… 🙂

    I have used above code and it is working and I am able to select folder.

    Facing one strange thing. Whenever I work in ISE, the select folder dialogue box went to last open item in windows instead of popping up on current window. But if I use powershell command line, dialogue box popping up and I can select folder.

  12. Avatar

    It'll be a function of where you call the dialog from.  Some apps (like the ISE) will remember the last folder you used, so, unless you specify a root every time, it will go to your last place.  

    Glad it worked out for you though 🙂  

    David F. 

  13. Avatar
    BETOMBO Mariot 4 years ago

    Very Usefull. Thanks

  14. Avatar
    Ségur marc 4 years ago

    hello how I can select a Folder?

  15. Avatar

    It's pretty much the exact same technique.. with a slightly different class.



  16. Avatar
    Denis 3 years ago

    Helpful article.  Thanks.

    Is there any method for tricking the OpenFileDialog into allowing me to select a folder?

    – I find the alternative [FolderBrowserDialog] far too awkward [I cannot see the folder hierarchy while I'm browsing, I can't paste in a path to get to where I want to go quickly, it's like peering through a keyhole]

  17. Avatar
    Denis 3 years ago


    That does not address my question at all.

    I asked about the OpenFileDialog . 

    I stated perfectly clearly that I did not want to use the FolderBrowserDialog. 

    Yet you post a link to a post about the FolderBrowserDialog .


  18. Avatar
    Denis 3 years ago

    The best I've been able to come up with is very much a workaround.

    Add-Type -AssemblyName system.Windows.Forms
    $SetBackupLocation = New-Object System.Windows.Forms.SaveFileDialog
    $SetBackupLocation.InitialDirectory = [Environment]::GetFolderPath('Desktop')
    $SetBackupLocation.Title = 'Choose folder - Enter a folder so it is selected in the NavPane then click on Save'
    $SetBackupLocation.FileName = 'Location'
    $rc = $SetBackupLocation.ShowDialog()
    if ($rc -eq [System.Windows.Forms.DialogResult]::OK)
    $BackupLocation = $SetBackupLocation.FileName
    $BackupLocation = $BackupLocation.Replace('Location', "")
    echo $BackupLocation
    Pause #To look at the result

    This came from Getting the file location using openfiledialog in powershell – TechNet

    As noted in the dialog title, the code returns the path of the folder that is selected in the Navigation Pane.

    I also found A better folderbrowserdialog – Sapien Forums but I could not get it to run. 


    • Avatar
      Leos Marek (Rank 4) 3 years ago

      Yeah I just wanted to write that you could select a file inside that folder and adapt the script to get the folder path from the file.. 🙂

      • Avatar
        Denis 3 years ago

        The posted code, which uses the SaveFileDialog, does not require a file to be selected.

        There will be cases where an empty folder is wanted so nothing based on selecting a file could be used.


        • Avatar
          Leos Marek (Rank 4) 3 years ago

          It was just an example, no need to catch every word 🙂 Seems that you already have a solution for the topic which is great.


    • Avatar
      Joe Stutter 1 year ago

      I got the code you mentioned here “I also found a better folder browser dialog – Sapien Forums but I could not get it to run. to work. The issue is on line 122. The original looks like this:

      for (int i = 1; i < names.Length; ++i) {
      	type = type.GetNestedType(names, BindingFlags.NonPublic);

      it should be:

      for (int i = 1; i < names.Length; ++i) {
      	type = type.GetNestedType(names[i], BindingFlags.NonPublic);

      names is an array and it is missing the counter [i] in the code.

  19. Avatar
    asdasda 3 years ago

    Now show how to open specific location not environment folder!

  20. Avatar
    Jason Spencer 1 year ago

    How would I handle if the user decides to click “cancel” on the open file dialog box? I’m guessing some kind of If / Else statement – but what condition can I check for?

    Right now, my code is expecting a valid file to be selected, and when I hit cancel, it tries to run off a $null expressioned Filebrowser.filename.

  21. Avatar

    You pretty much answered your own question. 🙂 Check if your result is $null first, and handle it as you need.

    David F.

    • Avatar
      Simmons 8 months ago

      I tried if ($fileBrowser -eq $null) { Exit } but that doesn’t work. how do you capture a cancel?

      • Avatar

        That may or may not work.. you normally check along the lines of “$result = $fbdialog.ShowDialog(); if ($null -eq $result) { #cancel pushed }”

        David F.

  22. Avatar
    Joshua Szanto 6 months ago

    Execution hangs up on usage of ShowDialog() under PowerShell 7 for me; it just stalls out. Another colleague does not have the same issue in PowerShell 7. Works fine in PowerShell 5.1 though. Any ideas? Been searching high and low and not sure why it stalls out upon executing ShowDialog() line in PoSh7.

  23. Avatar

    You might check out this article:
    Chrissy (author of DBATools) recommends that people use

    $appContext = New-Object System.Windows.Forms.ApplicationContext [void][System.Windows.Forms.Application]::Run($appContext)

    instead of ShowDialog()
    That might work for you?

    David F.

Leave a reply to Aniket Sonkusare Click here to cancel the reply

Please enclose code in pre tags

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


© 4sysops 2006 - 2023


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


Log in with your credentials


Forgot your details?

Create Account