Opening Documents with a File Explorer window using PowerShell

Do you find yourself working in PowerShell or running scripts and needing to open/import a document (normally CSVs) a lot, and realising you have a long string of a file path to type in to get to the file? Wouldn’t it be useful if you could bring up the File Explorer window to get to your documents? Well now you can!

Function Get-FileName($initialDirectory)
[System.Reflection.Assembly]::LoadWithPartialName("") |

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
} #end function Get-FileName

The function above can be added to the top of all your your scripts to be called at the appropriate point to open the file explorer window. So if you run:

$file = Get-FileName

The window will open (see below) and you can then browse for your file as you are used to, and it will be saved into the variable for use later in the script/session!


I have found the most useful way of implementing this function is by adding it to my PowerShell profile so it is available throughout my PowerShell session. If you don’t know how to configure your own PowerShell profile take a look at my earlier blog post here!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s