Skip to content

Instantly share code, notes, and snippets.

@talatham
Created June 12, 2013 14:51
Show Gist options
  • Save talatham/5765965 to your computer and use it in GitHub Desktop.
Save talatham/5765965 to your computer and use it in GitHub Desktop.
Open and read a file selected by a user dialog.
# ---------- FUNCTION -------------
Function Get-FileName
# Open file dialog box and select a file to import
{
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.filter = "Text Files (*.txt)| *.txt" # Set the file types visible to dialog
# Alternate filters include:
# "CSV files (*.csv) | *.csv"
$OpenFileDialog.initialDirectory = "c:\"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
# --------- USAGE ---------------
$filename = Get-FileName
$filecontents = Get-Content $filename
foreach ($line in $filecontents)
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment