Posts Tagged ‘browse’

Browse for File to POSH Variable

August 10, 2012 3 comments

After many months of getting tired of typing in an entire directory path when using the “Read-Host” command to import a text file or CSV file I went looking for commands to present a popup box to let me browse for a file, then have that file’s path be presented to a variable. This example shows that $file will be the file path for the file specified in the Get-FileName function.

A big thank you goes to the blog over at for showing me this wonderfulness ūüôā

Function Get-FileName($initialDirectory)


[System.Reflection.Assembly]::LoadWithPartialName("") |


$OpenFileDialog = <strong>New-Object</strong> System.Windows.Forms.OpenFileDialog

$OpenFileDialog.initialDirectory = $initialDirectory

$OpenFileDialog.Title = "PowerShell Master Says...Select File:"

$OpenFileDialog.filter = "All files (*.*)| *.*"

$OpenFileDialog.ShowDialog() | Out-Null


} #end function Get-FileName

# *** Entry Point to Script ***

$file = Get-FileName <em>-initialDirectory</em> "c:\"

%d bloggers like this: