Making a PowerShell LaunchPad Script

After I made a couple of PowerShell scripting tools that were meant to be shared among team members, I found that the process of getting them installed for everyone was quite tedious. Aside from informing people about the existence of a new tool, I had to then help them create shortcuts to each of them and also see if the scripts ran properly with their workstation’s current execution policy.

As more tools were created, everyone had to be aware of how to launch each of them. I wanted to avoid the manual processes and headache involved in this, so I thought that this idea of using a “launch pad” could kill two (or more) birds with one stone so to speak. The benefits I’ve seen from using this launch pad script are…

1) I only have to share a single script, one time, with team members. Once the short cut is created, any modifications or new scripts that are introduced will seamlessly appear within the launch pad tool.

2) The shortcut I am using has the execution policy bypass configured, to ensure the script will “just work” on anyones computer that I share this with. It is in there for convenience, and some folks may not want to use that.



The brief explanation of how this launch pad works is…

The script uses Get-ChildItem to gather a list of .ps1 files, and then dynamically adds each of them to a WinForms drop-down list within the tool. A user of the tool then selects the script they want, and clicks the Launch button which “dot sources” the .PS1 file they’ve chosen. From there, the chosen script simply “does what it does” as if they had manually navigated to and launched that script.



A more detailed explanation of the launch pad, including code snippets and screenshots…


First, you should setup a network share where all of your PowerShell tool scripts are to be accessed from.




If you prefer, you can put each script in individual sub-folders, such as when a script might need other companion files or for your own general house-cleaning preferences.




Then, place this LaunchPad script somewhere on the network, and create a new shortcut to the .PS1 file. Or, just right-click anywhere and choose New Shortcut.




Your shortcut should have the following properties configured, adjusted to reflect the location and file names of your launch pad script:

For the Target field:
C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File “\\Gbesso-w12r2\share\PowerShell-LaunchPad\PowerShell-LaunchPad.ps1”

For the Start in: field:
C:\Windows\System32\WindowsPowerShell\v1.0




Additionally, I like to edit the shorcut to have the PowerShell icon. Do this by clicking Change Icon, and mapping to:
C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe



The full .PS1 file and related items are on my newly setup GitHub storage, with that link below at the bottom of this page. A few of the key areas that are worth pointing out are…



# folder where you store the scripts you want to be included in the launch pad drop-down menu...
$global:lpLaunchFiles = '\\Gbesso-w12r2\share\PowerShellScripts'

This variable is used to store the location of your shared .ps1 files that the launch pad will link to.



#
# get list of files that the launchpad will list as options...
#
$lpFiles = Get-ChildItem "$global:lpLaunchFiles" -Filter *.ps1 -Recurse
$dropDownChoices = @()
$lpFiles | ForEach {
    $thisName = $_.BaseName
    $object1 = New-Object PSObject -Property @{
        Name=$thisName
    }
    $dropDownChoices += $object1
}
$dropDownChoices = $dropDownChoices | Sort-Object -Property Name

This section uses Get-ChildItem to gather all the .ps1 files in the directory you are choosing, and adds them to a new array. Then the array is sorted afterwards.



# looping through all ps1 files to populate the drop-down list...
$dropDownChoices | ForEach {
    $thisName = $_.Name
    $DropDownChoice.Items.Add("$thisName") | Out-Null
}

Within the form, after the drop-down menu is created, the array is used to add items to it.



$lpFiles | ForEach {
    $checkThis = $_.BaseName
    If ($checkThis -eq $choice) {
        $useDirectory =  $_.Directory
        $useFile = $_.Name
        ."$useDirectory\$useFile"
    }
}

When an item from the drop-down menu is selected, the OK button’s Add_Click section loops through the choices to see which one was selected. When it’s found, that item is “dot sourced” which calls the contents of the script (since these scripts have a function, but also a call to the local function underneath it).

 

On GitHub: https://github.com/gregbesso/PowerShell-LaunchPad

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