-
Notifications
You must be signed in to change notification settings - Fork 1
/
Show-GuiFilePicker.ps1
102 lines (78 loc) · 2.98 KB
/
Show-GuiFilePicker.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function Show-GuiFilePicker {
<#
.SYNOPSIS
Returns file objects from a GUI pop up window.
.DESCRIPTION
Displays a file picker window for the user, allowing non-technical users to leverage an automated routine.
.PARAMETER Title
A title for the pop up window
.PARAMETER InitialDirectory
Default path location of the picker window
.PARAMETER Extension
Filter the picker window by file type, supported: txt, csv, xml, pdf, xlsx, docx
.PARAMETER Single
Limit the picker window to a single selection
.PARAMETER outString
Limit the output to a string value(s)
.EXAMPLE
Show-GuiFilePicker -Title 'Select a File' -Single
#>
[CmdletBinding()]
param (
# A title for the pop up window
[Parameter()]
[string]
$Title = "Select File(s)",
# Default path location of the picker window
[Parameter()]
[string]
$InitialDirectory = "$($env:USERPROFILE)\Downloads",
# Filter the picker window by file type, supported: txt, csv, xml, pdf, xlsx, docx
[Parameter()]
[string]
$Extension,
# Limit the picker window to a single selection
[Parameter()]
[switch]
$Single,
# Limit the output to a string value(s)
[Parameter()]
[switch]
$outString
)
if (Test-Path $InitialDirectory) {
if($Extension -match '\.|\*') {
$Extension = $Extension -replace '\.' -replace '\*'
}
$Filter = switch ($Extension) {
'txt' {"Text Files (*.txt) | *.txt" ; break }
'csv' {"CSV files (*.csv) | *.csv" ; break }
'xml' {"XML files (*.xml) | *.xml" ; break }
'pdf' {"PDF files (*.pdf) | *.pdf" ; break }
'xlsx' {"Excel files (*.xlsx) | *.xlsx" ; break }
'docx' {"Word files (*.docx) | *.docx" ; break }
# Add in anything you want, like this:
'ps1' {"PowerShell scripts (*.ps1) | *.ps1" ; break }
Default {"All Files (*.*) | *.*"}
}
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.filter = $Filter
$OpenFileDialog.InitialDirectory = $InitialDirectory
$OpenFileDialog.RestoreDirectory = $true
$OpenFileDialog.Title = $Title
$OpenFileDialog.Multiselect = (!$Single)
$OpenFileDialog.CheckFileExists = (!$outString)
$OpenFileDialog.ShowDialog() | Out-Null
[string[]]$FullName = $OpenFileDialog.Filenames
if ($outString) {$FullName} else {Get-Item $FullName}
}else{
$msgSplat = @{
MessageTitle = "Path not Found!"
MessageBody = "The path $($InitialDirectory) cannot be found."
Button = 'OK'
Icon = 'Error'
}
Show-MessageBox @msgSplat
}
}