-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGet-InputFileDialogBox.ps1
More file actions
40 lines (31 loc) · 1.49 KB
/
Copy pathGet-InputFileDialogBox.ps1
File metadata and controls
40 lines (31 loc) · 1.49 KB
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
# This function will generate a File Explorer window to select your input file
# Allows you to specify the file type filter and default directory
Function Get-InputFileDialogBox {
Param(
[string]$DefaultPath = "C:\",
[string]$FileTypeFilter
)
Add-Type -AssemblyName System.Windows.Forms
# DefaultPath Environment Variables:
# Use 'dir env:' to list environment variables
# Use '[Environment]::GetEnvironmentVariable('$VariableName')' to use a variable from that list
# FileTypeFilter Variable Format:
# 'Dropdown Value Name'|'File Type filter'|..
# 'CSV (*.csv)|*.csv|Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx'
If ($FileTypeFilter) {
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = $DefaultPath
Filter = $FileTypeFilter
}
} Else {
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = $DefaultPath
}
}
# Displays dialog box (the .ShowDialog method does not return any values)
$null = $FileBrowser.ShowDialog()
# The selected file returns information in the $FileBrowser variable
$FilePath = $FileBrowser.FileName
return $FilePath
}
Get-InputFileDialogBox -DefaultPath $([Environment]::GetEnvironmentVariable('OneDriveCommercial')) -FileTypeFilter "CSV (*.csv)|*.csv|Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx"