Show-CommandGUI an enhanced Show-Command for PowerShell

After my last post (PowerShell tricks – Use Show-Command to add a simple GUI to your functions). I was thinking how one could write a function that would not have the deficiencies that Show-Command has when it comes to providing a GUI for functions. In addition to what Show-Command does I wanted a function that:

  • Considers advanced function parameters (i.e. ValidateScript, ValidatePattern, ValidateRange, ValideLength)
  • Populates the fields with the respective default values (if applicable)
  • Provides a browse for file or browse for folder option (if the parameter name contains ‘file’ or ‘folder’)
  • Provides meaningful error messages via message boxes
  • Doesn’t require the use of Invoke-Expression in order to run the parameters against the command
  • Can be customized to my own preferences

Say hello to Show-CommandGUI.

The screenshot above is produced with the following test function:

function test{
        [ValidateSet("sun", "moon", "earth")] 
        [ValidateScript({$_ -like '*test'})]
        [Parameter(Mandatory=$true, ParameterSetName='Advanced')]
        $filePath = 'c:\test.txt',
        [Parameter(Mandatory=$true, ParameterSetName='Basic')]
        $folderPath = 'c:\'
    "pattern: $pattern"
    "number: $number"
    "string: $string"
    if ($PSCmdlet.ParameterSetName -eq 'Advanced'){
        "switch: $switch"
        "filePath: $filePath"
        "choice: $choice"
        "folderPath: $folderPath"

Producing the GUI is as simple as this:

Import-Module "$PATHTOMODULE\Show-CommandGUI.psm1"
Show-CommandGUI test

The module and the test function can be downloaded via GitHub. Please let me know if you have any questions or ideas on how to improve the function.


Photo Credit: Fernando X. Sanchez via Compfight cc


I'd love to hear what you think

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

You are commenting using your 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