Open File Dialog Box

Often when working with PowerShell you will want to specify a file to read data from it.  Whilst the file path can be hard-coded into your script, or passed as a parameter it is often more convenient to take advantage of windows functionality that people have become used to.

The code below allows you to call one of the standard windows libraries to open the dialog box used for choosing files.  The code comes from a technet article which explains what each line of code does should you be interested.  The article can be found at Use a .NET Framework Class to Open a File Dialog Box

 

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
$openFile = New-Object System.Windows.Forms.OpenFileDialog
$openFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
If($openFile.ShowDialog() -eq "OK") {get-content $openFile.FileName}

Leave a Reply

Your email address will not be published. Required fields are marked *