Software menu item with open command

Creating an Open File Dialog in PowerShell

Recently I created a PowerShell function to open a open file dialog window, allowing you to browse to that csv file or read in a text file without specifying the path.

As this has found it’s way into several of my script’s already, it feels worthy enough to share with others.

Function Open-FileDialog {
	<#
        .SYNOPSIS
        Creates an Open File Dialog.

        .DESCRIPTION
        Creates an Open File Dialog.
        Takes a custom filter for file extentions as well as starting in a different directory.

        .PARAMETER Filter
        Specifies the file extention filter. Defaults to Documents (*.docx) and SpreadSheets (*.xlsx)

        .PARAMETER InitialDirectory
        Specifies the initial directory. "Desktop" is the default.
		
		.PARAMETER Title
        Specifies the title of the open file dialog box.
		
		.PARAMETER Multiselect
        Specifies if multi file select is enabled. False is the default.

        .INPUTS
        None. You cannot pipe objects to Open-FileDialog.

        .OUTPUTS
        System.Windows.Forms.OpenFileDialog. Open-FileDialog returns a string with the extension or file name.

        .EXAMPLE
        PS> $file = Open-FileDialog
		PS> $file.FileName
        C:\Temp\File.txt
		
		.EXAMPLE
        PS> $file = Open-FileDialog -Title "My Title"
		PS> $file.FileName
        C:\Temp\File.txt
		
		.EXAMPLE
        PS> $file = Open-FileDialog -Title "My Title" -Multiselect
		PS> $file.FileNames
        {C:\Temp\File.text,C:\Temp\File2.txt}
		
        .EXAMPLE
        PS> $csv = Import-CSV (Open-FileDialog -InitialDirectory "C:\Temp" -Filter "CSV|*.csv").FileName
        File.doc

        .LINK
        Online version: https://www.chriscolden.net/2023/03/20/creating-an-open-file-dialog-in-powershell/
    #>
	
	Param (
        [Parameter(Mandatory=$false)] [String]$Filter = 'Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx',
		[Parameter(Mandatory=$false)] [String]$InitialDirectory = [Environment]::GetFolderPath('Desktop'),
		[Parameter(Mandatory=$false)] [String]$Title = $null,
		[Parameter(Mandatory=$false)] [Switch]$Multiselect = $false
    )
	
	if (-not ([System.AppDomain]::CurrentDomain.GetAssemblies() | ?{$_.Location -match "System.Windows.Forms"}))
	{
		Add-Type -AssemblyName System.Windows.Forms
	}
	
	$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
		InitialDirectory = $InitialDirectory 
		Filter = $Filter
		Multiselect = $Multiselect
		Title = $Title
	}
	$null = $FileBrowser.ShowDialog()
	
	Return $FileBrowser
}

To show what this function returns we can simply call it, select a file and it will return the System.Windows.Forms.OpenFileDialog object to the screen.

PS C:\Users\Chris.Colden> Open-FileDialog

CheckFileExists              : True
Multiselect                  : False
ReadOnlyChecked              : False
ShowReadOnly                 : False
SafeFileName                 : New Text Document.txt
SafeFileNames                : {New Text Document.txt}
AddExtension                 : True
CheckPathExists              : True
ClientGuid                   :
DefaultExt                   :
DereferenceLinks             : True
FileName                     : C:\Temp\New Text Document.txt
FileNames                    : {C:\Temp\New Text Document.txt}
Filter                       : Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx
FilterIndex                  : 1
InitialDirectory             : C:\Users\Chris.Colden
RestoreDirectory             : False
ShowHelp                     : False
SupportMultiDottedExtensions : False
Title                        :
ValidateNames                : True
CustomPlaces                 : {}
AutoUpgradeEnabled           : True
Tag                          :
Site                         :
Container                    :

This isn’t very useful on it’s own, so let me show you some things we can do to put this function to good use.

For example, lets use the Open-FileDialog function to import a csv file.

PS C:\Users\Chris.Colden> $csv = Import-CSV (Open-FileDialog -InitialDirectory "C:\Temp" -Filter "CSV|*.csv").FileName
PS C:\Users\Chris.Colden> $csv

LatD  : 41
LatM  : 5
LatS  : 59
NS    : N
LonD  : 80
LonM  : 39
LonS  : 0
EW    : W
City  : Youngstown
State : OH

LatD  : 42
LatM  : 52
LatS  : 48
NS    : N
LonD  : 97
LonM  : 23
LonS  : 23
EW    : W
City  : Yankton
State : SD

As you can see, we have successfully imported the csv file I selected, which just happened to have some city information in it.

That about wraps it up for now. I hope you find this function useful.

Happy PowerShelling!

You may also like