I’ve been playing with Windows 8 store apps (metro apps) in C# and Xaml recently, and of course I thought it would be cool to launch these apps from within Windows PowerShell. After some fruitless research trying to find a native way to do this in PowerShell or C#, I learned about a C++ interface called IApplicationActivationManager.
Fortunately, I was able to find some code on Stack Overflow that showed how to use this in C# (the post also listed the registry information I will describe in this article). After some tweaking the code and the Add-Type syntax, I was able to expose the function I needed into PowerShell. After that it was only a matter of wrapping the function into something that resembles PowerShell.
The code can be found on poshcode.
This is a simple function that returns all of the metro apps installed on your machine. Any filtering must be done with Where-Object. Here’s a sample of what the cmdlet looks like when run:
23:48:22 PS D:\dropbox\scripts> Get-MetroApp |ft -AutoSize
The code behind this function is all about the registry. However, this is not an easy story to tell. The registry keys are not consistent. Basically, xaml apps are shows differently than HTML5 apps. Also, I’m still not entirely sure what DirectX apps will look like, but I assume that the Chrome browser is a C++ app so hopefully we’re covered there. Here is the logic behind what Get-MetroApp looks for in the registry:
The starting point for everything is in HKCU:\Software\Classes\ActivatableClasses\Package. Each key is a separate app. Each of these keys contains a Sever key and a key called ActivatableClassID.
The server key contains two keys. The one of these that is not named BackgroundTransferHost.1 is the one that will have a property called AppUserModelID. The value of this is what is needed in order to launch a metro app. This is the ID property returned from Get-MetroApp.
Unfortunately, it’s not always obvious which app is which from this key so the Get-MetroApp also looks in the ActivatableClassID key for another key that has a CustomAttributes key with a property named AppObject.EntryPoint. I found that the best key to try first was one called App if it existed, followed by app.wwa. If neither of those is found, I iterate through each key looking for the AppObject.EntryPoint property.
In the case of xaml apps, the value of Appobject.Entrypoint appears to show the name of the app. In html5 apps, it appears to only show default.html. That is, unless there are other keys or sources out there that we (the community) have yet to discover. In all of the apps that I had installed, I was able to tell which app was which by reading one or the other property returned by Get-MetroApp.
You can ether pass an ID returned by Get-MetroApp
or you can pipe one of the results from get-metroapp into Start-MetroApp.
Get-MetroApp |? entrypoint -match metrotwit |Start-MetroApp
I used the following to test that it was able to start every app i had installed:
I know this will come in handy to many. I have already added the module in my profile and have bound a few functions to allow me to quickly switch to specific metro apps very quickly. However, I’m not sure this is over yet. I’m curious if we will find better ways to grab the name of the app and link it with the ID. Please feel free to use the comments section of this page if you learn anything beyond what I have done.
Edit: I just learned about Get-AppxPackage in the Appx module. It appears to have a cleaner name, but it does not have the launch id required to launch the app. I’m sure it will be easy to correlate the packages to this name using some of the other keys. This will have to wait for another day, but it’s at least a lead to a better Get-MetroApp cmdlet.