#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: FUNCTION Show-OpenFileDialog

In this post, I’ll show a function I created for using the Open File Dialog within PowerShell. This gives you a quick way to select a document on your local system.

FUNCTION Definition

The following is the function wrapper which will contain the remainder of the code.
  Used to select a local file.
  Used to select a local file using the Windows Open File Dialog.
  Demonstrates using the Show-OpenFileDialog cmdlet to select a local file without any parameters.
  $LocalFile = Show-OpenFileDialog -Verbose
  Demonstrates using the Show-OpenFileDialog cmdlet to select a local file with the InitialDiretory and FileExtension parameters.
  $LocalFile = Show-OpenFileDialog -InitialDirectory 'c:\temp\export' -FileExtension 'xlsx' -Verbose
  Returns the full path and file name of the local file selected.
FUNCTION Show-OpenFileDialog {

    param (...) # end param...

    BEGIN {...} # end BEGIN...

    PROCESS {...} # end PROCESS...

    END{...} # end END...
} # end FUNCTION Get-MyPWDocumentsByUpdatedDate...
Export-ModuleMember -Function Show-OpenFileDialog


First thing we need to do is create our parameter and value pairs. The help messages will explain the purpose for each parameter. You will see that I have incorporated “ValidateScript” for the InitialDirectory paramater. This way I do not have do any additional checks to determine if a specified local folder exists.
param ( 
    # Specifies the initial local folder to be used within the dialog.
    [ValidateScript({Test-Path -Path $_ })]
    [string] $InitialDirectory,

    # Specifies the file extension to filter on.
    [string] $FileExtension
) # end param...


I want to take a second and point out a couple of conventions that I use in my scripts. First, you may notice that I put a comment at the end of many of the blocks of code. Doing this makes it easier for me to determine where a block of code starts and ends. Comes in handy when troubleshooting a script.  I also add text ([BEGIN], [PROCESS], [END]) in the output messages (Write-Verbose, Write-Warning, Write-Error) corresponding to the section of the script I am in. Again, this makes it obvious where something occurs within the script.
    $CmdletName = $MyInvocation.MyCommand.Name
    $StartTime = Get-Date
    Write-Verbose -Message "[BEGIN] $StartTime - Entering '$CmdletName' Function..."

    # Import the required assembly.
| Out-Null

} # end BEGIN...


Now, we will proceed to the PROCESS code block.
    try {
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog

        # Set the initial folder if one is provided.
        if( -not ([string]::IsNullOrEmpty($InitialDirectory))){
            $OpenFileDialog.InitialDirectory = $initialDirectory

        # Set the filter if a file extension is provided.
        $filter = “All files (*.*)| *.*
        if( -not ([string]::IsNullOrEmpty($FileExtension))){
            $filter = "Custom (*.$FileExtension)|*.$FileExtension"

        $OpenFileDialog.Filter = $filter
        $OpenFileDialog.ShowDialog() | Out-Null
        # Output the file path and name for the selected file.
        Write-Output $OpenFileDialog.FileName 
    } catch {
        Write-Warning -Message "[PROCESS] Something went wrong with the OpenFileDialog.
    } # end try/catch...
} # end PROCESS...


Lastly, we will proceed to the END block of code.

    $EndTime = Get-Date
    Write-Verbose -Message "[END] It took $($EndTime - $StartTime) to complete the process."
    Write-Verbose -Message "[END] $EndTime - Exiting '$CmdletName' Function..."

} # end END...


The following is a simple example usage of the Show-OpenFileDialog. openpowershell
Experiment with it and have fun. Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.