Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Last active November 4, 2021 04:44
Show Gist options
  • Save steviecoaster/cd109ce9cd07a52b3fc6c28b9bd3829b to your computer and use it in GitHub Desktop.
Save steviecoaster/cd109ce9cd07a52b3fc6c28b9bd3829b to your computer and use it in GitHub Desktop.
Powershell Open File Dialog
#Thomas Rayner is awesome. Everyone should be like Thomas. Mostly ganked from here: https://thomasrayner.ca/open-file-dialog-box-in-powershell/
function Open-FileDialog {
[cmdletBinding()]
param(
[Parameter()]
[ValidateScript({Test-Path $_})]
[String]
$InitialDirectory
)
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
if($InitialDirectory){
$FileBrowser.InitialDirectory = $InitialDirectory
}
else{
$fileBrowser.InitialDirectory = [Environment]::GetFolderPath('MyDocuments')
}$FileBrowser.Filter = 'SpreadSheet (*.xlsx)|*.xlsx|CSV (*.csv)|*.csv|All Files (*.*)|*.*'
[void]$FileBrowser.ShowDialog()
$FileBrowser.FileName
}
$file = Open-FileDialog C:\tmp
$content = Get-Content $file
$content
@CaoNghia197
Copy link

10799

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment