#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.
<#
.Synopsis
  Used to select a local file.
.DESCRIPTION
  Used to select a local file using the Windows Open File Dialog.
.EXAMPLE
  Demonstrates using the Show-OpenFileDialog cmdlet to select a local file without any parameters.
  $LocalFile = Show-OpenFileDialog -Verbose
.EXAMPLE
  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
.OUTPUTS
  Returns the full path and file name of the local file selected.
#>
FUNCTION Show-OpenFileDialog {

    [CmdletBinding()]
    param (...) # end param...

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

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

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

Parameters

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.
    [ValidateNotNullOrEmpty()]
    [ValidateScript({Test-Path -Path $_ })]
    [Parameter()]
    [string] $InitialDirectory,

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

BEGIN

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.
BEGIN {  
    $CmdletName = $MyInvocation.MyCommand.Name
    $StartTime = Get-Date
    Write-Verbose -Message "[BEGIN] $StartTime - Entering '$CmdletName' Function..."

    # Import the required assembly.
     [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) 
| Out-Null

} # end BEGIN...

PROCESS

Now, we will proceed to the PROCESS code block.
PROCESS {
    
    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...

END

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

    $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...

EXAMPLE

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 comment

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