20

#PSTip: Using the System.Windows.Forms.FolderBrowserDialog Class

Note: This tip requires PowerShell 2.0 or above.

In PowerShell, it is possible to use GUI elements to allow for user input during scripts. Although it is possible to create your own forms from scratch, there are also many useful pre-built dialogs available. In this tip, we will see how the System.Windows.Forms.FolderBrowserDialog can be used to select a folder or path that can be used within a script.

On the first line we add the System.Forms class. This is to ensure the assembly is loaded. The New-Object Cmdlet creates the actual form and ShowDialog() Method on the object invokes the folder browser dialog.

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath

BrowseForFolder

It is also possible to specify in which folder the folder browser starts. By default this is the user’s desktop but this can be changed by modifying the SelectedPath property:

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    SelectedPath = 'C:\Temp’
}

[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath

To limit the folders the RootFolder property can be utilized, it should be noted that only special folders can be entered here, a folder of the [System.Environment+SpecialFolder] type. This means a custom path cannot be entered here. In the next code sample I have also disable the New Folder property, this is to ensure a user cannot create a folder and then select that folder:

Add-Type -AssemblyName System.Windows.Forms

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    RootFolder = 'MyDocuments'
    ShowNewFolderButton = $false
}

[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath

To get a full list of the available special folders the [enum] accelerator can be used to enumerate these folders. This code will generate list of the available values for the RootFolder property:

[Enum]::GetNames([System.Environment+SpecialFolder])

To learn more about this class or the values in the RootFolder property the following two MSDN articles can be used:

http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.95).aspx

Filed in: Columns, Tips and Tricks Tags: , , , ,

20 Responses to "#PSTip: Using the System.Windows.Forms.FolderBrowserDialog Class"

  1. Kovrigin says:

    Big thx! It is extremely useful

    • Jaap Brasser says:

      Glad that you liked it, in what scenario are you using this?

      • adotms says:

        Save logs files

      • Deepak says:

        HI Jaap,
        I am trying to create a manifest checker for that we need to browse a folder and only the folder should be selected it shouldnot show options for selecting the files.I will be very much thankful if you can just help me out..

        • Jaap Brasser says:

          Hello Deepak,

          I am not sure that I understand your question correctly, could you please explain what you are trying to achieve here. I would be happy to help you out.

        • Rajat says:

          Hi Deepak

          The above mentioned script will do exactly what you require.
          I will only browse till folder level and not files level.

  2. mariu5 says:

    Pretty cool and all but the user can still create a new folder by right clicking on a parent folder and then select “new”.
    Is there by any chance a way to disable this as well?

  3. William Robinson says:

    Thanks, this was exactly what I needed. A lot easier than anything else I found previously.

  4. James says:

    I run the sample and when the ShowDialog() is executed, no dialog is displayed. The method appears hung or waiting. Any suggestions?

  5. Armin says:

    Hi anyone.

    We also experience the same behaviour as james described. ISE also works fine, but the powershell window itself hangs and does nothing.

    on my pc it works (PS version 3)
    on my colleagues pc (PS version 2) it fails as described by James before.

    Did anyone find a solution yet?

  6. Cliff says:

    For all that this method appears to hang it is because the dialog is opening ib the background behind all windows. Have not found a fix yet.

  7. Asnivor says:

    This is appears to be an issue only in PS v 2.0. Even on later versions of PowerShell you can reproduce the problem by forcing version 2.0. eg:

    powershell -version 2.0 -file .\SomeScriptWithSystemForms.ps1

    In order to get this working from the 2.0 console is to force Single Threaded Apartment Mode when you call the script.

    So on powershell version 2.0:

    powershell -sta -file .\SomeScriptWithSystemForms.ps1

    This enables the dialog to be shown when running the script from the console.

  8. Jeff says:

    Really nice and simple. Two issues:
    1.) How do you get the dialog to appear in front of the ISE?
    2.) Tried to use some of the other special folder types but get an error:

    Add-Type -AssemblyName System.Windows.Forms | Out-Null
    $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    RootFolder = [Environment]::GetFolderPath(“ProgramFiles”)
    ShowNewFolderButton = $false
    }

  9. Brad says:

    Thanks Jaap,
    This will be perfect for my backup and restore script that I am looking to push out to 1800~ staff and students.
    They will select thier backup drive and then ill script a backup of all the important files and have one for the restore after a reimage or new device.
    Awesome work!

  10. Brad says:

    Thanks Jaap,
    This will be perfect for my backup and restore script that I am looking to push out to 1800~ staff and students.
    They will select their backup drive and then ill script a backup of all the important files and have one for the restore after a reimage or new device.
    Awesome work!

    • Jaap Brasser says:

      Hello Brad,

      It is always nice to get feedback from the community. If you need any pointers or feedback on your backup script be sure to visit the PowerShell.com forums, there are a lot of experts there willing to help out with scripting questions.

      Happy scripting!

  11. Moe says:

    Hi

    Is it possible to hide specific Drives? Fo example just show Letter C:\ , D:\ & E:\ ?

    thx for answer 🙂

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: