30 lines
1022 B
PowerShell
30 lines
1022 B
PowerShell
# Note: store in your profile for easy use
|
|
# Example use:
|
|
# $file = Select-FileDialog -Title "Select a file" -Directory "D:\scripts" -Filter "Powershell Scripts|(*.ps1)"
|
|
|
|
function Select-FileDialog
|
|
{
|
|
param([string]$Title,[string]$Directory,[string]$Filter="All Files (*.*)|*.*")
|
|
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
|
|
$objForm = New-Object System.Windows.Forms.OpenFileDialog
|
|
$objForm.InitialDirectory = $Directory
|
|
$objForm.Filter = $Filter
|
|
$objForm.Title = $Title
|
|
$Show = $objForm.ShowDialog()
|
|
If ($Show -eq "OK")
|
|
{
|
|
Return $objForm.FileName
|
|
}
|
|
Else
|
|
{
|
|
Write-Error "Operation cancelled by user."
|
|
}
|
|
}
|
|
|
|
$file = Select-FileDialog -Title "Select a file" -Directory "D:\scripts" -Filter "Powershell Scripts (*.ps1)|*.ps1"
|
|
|
|
#multiple file category arguments should not include extra space:
|
|
#-Filter “Text files (*.txt) | *.txt | All files (*.*) | *.*”
|
|
#does not work — only show *.* filter.
|
|
#-Filter “Text files (*.txt)|*.txt|All files (*.*)|*.*”
|
|
#works. |