Created
June 12, 2013 14:51
-
-
Save talatham/5765965 to your computer and use it in GitHub Desktop.
Open and read a file selected by a user dialog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------- 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