Filedialog and Powershell

Filedialog and Powershell

When I ask any input from a user in Powershell script most of the time I ask it in a parameter of the script body or just prompt for it during runtime. But with the underlying .NET Framework you can take advantage of the GUI, for example using a dialog panel. By using a dialog to specify a file you can be sure, that there is no spelling mistake in the path and you also provide a better user experience.

Open a file...
Open a file…

Use OpenFileDialog class from the System.Windows.Forms namespace to ask a user to specify a file. Read more about this at MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog%28v=vs.100%29.aspx

To work with this class, first load the namespace with the following command.

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

If you want to get rid of the message after this command just redirect the output with pipe or put it all to a variable:

$loadNamespace = [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null

Next you can initialize an instance of this class using New-Object, and because the constructor does not require any parameters use this command:

$myDialog = New-Object System.Windows.Forms.OpenFileDialog

To ask some help about this object just use Get-Member commadlet, any it prints you all methods and properties for OpenFileDialog

$myDialog | Get-Member

System.Windows.Forms.OpenFileDialog members
System.Windows.Forms.OpenFileDialog members

Also just type the variable name and you will see the current (default) values for properties.

$myDialog

Alter these to meet your requirements in your scenario.

For example set the title of the window:

$myDialog.Title = “Window title text”

And the actual directory:

$myDialog.InitialDirectory = “C:\Windows”

And add some file type filters, for example text files:

$myDialog.Filter = “Text files (*.txt)|*.txt”

or all files:

$myDialog.Filter = “All Files (*.*)|*.*”

If you finished with configuring the outfit of the window and added all parameters, use ShowDialog() to pop it up to the user:

$result = $myDialog.ShowDialog()

In the $result variable Powershell stores the return value, based on the user pressed the Open button (the result is OK string), the cancel button or just closed the window (in both cases the result is Cancel string). So just check $result for these strings and act accordingly.

By the way, the file or files’ names are in the FileName and FileNames properties, now you can work with them.

OpenFileDialog with filename provided
OpenFileDialog with filename provided

Full code example:

$myDialog = New-Object System.Windows.Forms.OpenFileDialog

$myDialog.Title = “Please select a file”

$myDialog.InitialDirectory = “C:\Windows”

$myDialog.Filter = “All Files (*.*)|*.*”

$result = $myDialog.ShowDialog()

If($result -eq “OK”) {

$inputFile = $myDialog.FileName

# Continue working with file

}

else {

Write-Host “Cancelled by user”

}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s