Last active
July 3, 2024 22:13
-
-
Save webtroter/37bb0b34a5ce7a1e83ffd6af139cefb9 to your computer and use it in GitHub Desktop.
Open/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
| function Get-SaveFilePath { | |
| [CmdletBinding()] | |
| param ( | |
| [string] | |
| $InitialDirectory = $PWD, | |
| [string] | |
| $Filter | |
| ) | |
| begin { | |
| [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null | |
| } | |
| process { | |
| $FileBrowser = New-Object System.Windows.Forms.SaveFileDialog -Property @{ | |
| InitialDirectory = $initialDirectory | |
| Title = "Save File" | |
| Filter = if ($PSBoundParameters.ContainsKey('Filter')) { | |
| "$Filter|All Files (*.*)|*.*" | |
| } else { | |
| "All Files (*.*)|*.*" | |
| } | |
| } | |
| $FileBrowser.ShowDialog() | Out-Null | |
| Write-Output $FileBrowser.FileNames | |
| } | |
| end { | |
| } | |
| } | |
| function Get-OpenFilePath { | |
| [CmdletBinding()] | |
| param ( | |
| [string] | |
| $InitialDirectory = $PWD, | |
| [string] | |
| $Filter = "All Files (*.*)|*.*" | |
| ) | |
| begin { | |
| [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null | |
| } | |
| process { | |
| $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ | |
| InitialDirectory = $initialDirectory | |
| Title = "Select Files to open" | |
| Multiselect = $true | |
| Filter = if ($PSBoundParameters.ContainsKey('Filter')) { | |
| "$Filter|All Files (*.*)|*.*" | |
| } else { | |
| "All Files (*.*)|*.*" | |
| } | |
| } | |
| $FileBrowser.ShowDialog() | Out-Null | |
| Write-Output $( [PSCustomObject]@{ | |
| FileName = $FileBrowser.FileName | |
| FileNames = $FileBrowser.FileNames | |
| }) | |
| } | |
| end { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment