How to use open file dialog in PowerShell

When you are working with files sometimes is easier use the open file dialog or select the files.

For example when you use the Import-Csv cmdlet to retrieve data from a csv you can use these lines:

$File = New-Object System.Windows.Forms.OpenFileDialog -Property @{

    InitialDirectory = [Environment]::GetFolderPath(‘Desktop’)

}

$null = $File.ShowDialog()

$FilePath = $File.FileName

Import-Csv $FilePath

Then work with all data in the Csv file.

The most important part of this code is the class System.Windows.Forms.OpenFileDialog that allows create the object $File

I have another post working with .csv files to see how process the records but for now I want to show you this nice feature.

Thanks – Gracias  – Dhanyavaad

Leave a comment