Triggering a PowerShell script from a website using PSCommander

PowerShellProTools PSCommander

April 7, 2021

quote Discuss this Article

PSCommander allows you to command your desktop with PowerShell. In the 1.2 version of commander, we’ve released a feature that allows you to configure custom protocol handlers. A custom protocol handler enables the ability to trigger an application whenever a particular protcol is invoked.

For example, you can think about how Zoom or Microsoft Teams provides a web page that then can open the local application to join the meeting. What’s happening is that the URL that the web is opening uses a custom protocol with some information about the meeting to join.

You may see a link a URL such as:

zoom://meeting/12312312

Without a custom protocol handler, Windows won’t know how to service this protocol request. After registering a protocol handler, Windows will open the application and provide the link that has been clicked.

Creating a Protocol Handler with PSCommander

To create a custom protocol handler with PSCommander, you can use the New-CommanderCustomProtocol cmdlet within your commander config.ps1 file. You’ll need to define the protocol and then the action to take when that protocol is invoked. The $args[0] variable will contain the data included with your protocol.

This example defines a myApp:// protocol that accepts a couple of different applications to launch. The action script is invoked any time the protocol is called.

New-CommanderCustomProtocol -Protocol myApp -Action {
     if ($args[0] -eq 'notepad') { Start-Process notepad }
     if ($args[0] -eq 'calc') { Start-Process calc }
     if ($args[0] -eq 'wordpad') { Start-Process wordpad }
}

To take advantage of this protocol, you could create a web page with specially crafted links that invoke the protocol. A standard HTML anchor tag can be used with the protocol URL.

This example creates a basic HTML page with a single anchor that will open notepad when clicked.

<html>
  <body>
    <a href="myApp://notepad">Start Notepad</a>
  </body>
</html>

As you can see, Microsoft Edge will warn that the website is attempting to us PSCommander to open a myApp:// link. This is common and holds true for other protocol handlers, like Zoom. Once the prompt is accept, the protocol handler will be invoked and notepad will open.

Try it today!

You can download and try PSCommander today. It’s available on the PowerShell Gallery.

Install-Module PSCommander