Last active
February 4, 2020 02:58
-
-
Save josheinstein/167121889c25c20453f7 to your computer and use it in GitHub Desktop.
A PowerShell module for showing common dialogs. (Currently only supports open and save file dialogs.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
# MODULE | |
Description = "Common Dialogs Module" | |
ModuleVersion = '1.0' | |
GUID = '374EF8CD-AEC6-464C-92DA-290C662DA183' | |
# AUTHOR | |
Author = 'Josh Einstein' | |
# REQUIREMENTS | |
PowerShellVersion = '3.0' | |
CLRVersion = '4.0' | |
RequiredModules = @() | |
RequiredAssemblies = @('PresentationFramework') | |
# CONTENTS | |
FormatsToProcess = @() | |
TypesToProcess = @() | |
ModuleToProcess = 'Dialogs.psm1' | |
NestedModules = @() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#.SYNOPSIS | |
# Shows a file open or save dialog box and returns the selected file name. | |
function Select-File { | |
[CmdletBinding(DefaultParameterSetName='Open')] | |
param( | |
# The initial file name | |
[Parameter(Position=1)] | |
[String]$FileName, | |
# The caption of the dialog. | |
[Parameter()] | |
[String]$Title, | |
# The initial directory that is displayed in the dialog. | |
[Parameter()] | |
[String]$InitialDirectory, | |
# Prompts the user to create a new file or save to an existing file. | |
[Parameter(ParameterSetName='Save')] | |
[Switch]$Save, | |
# Prompts the user to open an existing file or files. | |
[Parameter(ParameterSetName='Open')] | |
[Switch]$Open, | |
[Parameter(ParameterSetName='Open')] | |
[Switch]$AllowMultiple, | |
# An array of file types to display in the drop down list in the | |
# dialog. The syntax of the strings is as follows: | |
# Description|mask[;mask;mask...] | |
# The default is 'All Files (*.*)|*.*' | |
[Parameter()] | |
[String[]]$FileTypes = @( | |
'All Files (*.*)|*.*' | |
) | |
) | |
process { | |
if ($PSCmdlet.ParameterSetName -eq 'Save') { | |
$Dialog = New-Object Microsoft.Win32.SaveFileDialog | |
$Dialog.OverwritePrompt = $true | |
} | |
else { | |
$Dialog = New-Object Microsoft.Win32.OpenFileDialog | |
$Dialog.CheckFileExists = $true | |
if ($AllowMultiple) { | |
$Dialog.Multiselect = $true | |
} | |
} | |
if ($Title) { $Dialog.Title = $Title } | |
else { $Dialog.Title = $PSCmdlet.ParameterSetName + " File" } | |
# Convert any PowerShell paths to file system paths | |
if ($FileName) { $FileName = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FileName) } | |
if ($InitialDirectory) { $InitialDirectory = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($InitialDirectory) } | |
# If they specified a filename but not initial direcotry, | |
# use the path of the file as the initial directory. | |
# Otherwise use the current $PWD working directory | |
if (!$InitialDirectory) { | |
if ($FileName) { | |
$InitialDirectory = [System.IO.Path]::GetDirectoryName($FileName) | |
$FileName = [System.IO.Path]::GetFileName($FileName) | |
} | |
else { | |
$InitialDirectory = $PWD.ProviderPath | |
} | |
} | |
$Dialog.FileName = $FileName | |
$Dialog.InitialDirectory = $InitialDirectory | |
$Dialog.Filter = $FileTypes -join '|' | |
$Dialog.CheckPathExists = $true | |
if ($Dialog.ShowDialog()) { | |
if ($AllowMultiple) { | |
Write-Output $Dialog.FileNames | |
} | |
else { | |
Write-Output $Dialog.FileName | |
} | |
} | |
} | |
} | |
Export-ModuleMember -Function *-* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment