Last active
November 4, 2021 04:44
-
-
Save steviecoaster/cd109ce9cd07a52b3fc6c28b9bd3829b to your computer and use it in GitHub Desktop.
Powershell Open File Dialog
This file contains 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
#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
commented
Nov 4, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment