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.
Get-MetroApp
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 EntryPoint ID ---------- -- default.html 26720RandomSaladGamesLLC.HeartsDeluxe_kx24dqmazqk8j!App Magic.UltimateTuner.App 26739TheMagicSoftware.UltimateTuner_hf01bqnspr91a!App ContosoCookbook.App 4d1ddc26-6768-4a0d-97db-7cd72fc805c9_d6s28v2r30d6p!App DataBindingTest.App 51da487c-5876-4ec7-9cad-fb5762b5032c_d6s28v2r30d6p!App Tuner.App 76f04ed9-49c3-434c-8d45-e392388e7e4d_d6s28v2r30d6p!App BoxeeRemote.App 8c2b4686-67a7-46c2-aa34-5ac085571fed_d6s28v2r30d6p!App Microsoft.Expression.DesignHost.Isolation.IsolationProcess App.a875b8f2e.a8a0c.a437b.aa93f.a4b3cf406091a_8wekyb3d8bbwe!Designer.App DefaultBrowser.DefaultBrowserActivatableClass DefaultBrowser_NOPUBLISHERID!Chrome HSW.Client.App DiscoveryCommunicationsIn.HowStuffWorks_qa3esp0sj9xn6!App default.html Microsoft.BingWeather_8wekyb3d8bbwe!App default.html Microsoft.SkypeApp_kzf8qxf38zg5c!App MetroTwit.App PixelTuckerPtyLtd.MetroTwit_5kbmb3e034y6r!App
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.
Start-MetroApp
You can ether pass an ID returned by Get-MetroApp
Start-MetroApp PixelTuckerPtyLtd.MetroTwit_5kbmb3e034y6r!Ap
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:
Get-MetroApp |%{Start-MetroApp;sleep5}
The End?
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.
