PS OpenFileDialog

Hello together,

here a simple smart function to show an open file dialog:

function Show-OpenFileDialog
{
    <#
            .SYNOPSIS
            Shows up an open file dialog.
            .EXAMPLE
            Show-OpenFileDialog
    #>
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false, Position=0)]
        [System.String]
        $Title = 'Windows PowerShell',
        
        [Parameter(Mandatory=$false, Position=1)]
        [Object]
        $InitialDirectory = "$Home\Documents",
        
        [Parameter(Mandatory=$false, Position=2)]
        [System.String]
        $Filter = 'PowerShell-files|*.ps1|Everything|*.*'
    )
    
    Add-Type -AssemblyName PresentationFramework
    
    $dialog = New-Object -TypeName Microsoft.Win32.OpenFileDialog
    $dialog.Title = $Title
    $dialog.InitialDirectory = $InitialDirectory
    $dialog.Filter = $Filter
    if ($dialog.ShowDialog())
    {
        $dialog.FileName
    }
    else
    {
        Throw 'Nothing selected.'    
    }
}

Show-OpenFileDialog

Have fun with it!

Greetings,

David

Advertisements

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