Question

I've been trying to figure this out for a while, but no luck, I would like to pop up a file open dialog, which I have been able to do with a function I found online, and then use the file location further down the line in my script, but I can't figure it out.

Here is the function

Function Get-FileName()
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = "C:\Users\$($env:username)\Documents\"
 $OpenFileDialog.filter = "CSV's (*.csv)| *.csv"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
}

I'm new to powershell so I'm not sure how functions work.

Was it helpful?

Solution

Keep it in a variable and use it whenever you like.

$location = Get-FileName
copy $location c:\temp
start $location
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top