Installing SharePoint 2013 apps with PowerShell

Posted Thursday, February 28, 2013 11:00 AM by CoreyRoth

My PowerShell posts always prove to be the most popular.  I showed all sorts of cool PowerShell tricks in my talk back at SPC12.  One of the things I covered was how to install and manage apps using PowerShell.  Since there is a corporate catalog and SharePoint store, you might not think you need to install apps with PowerShell, but your developers may choose to the app model for a future internal project, so as an IT Pro you need to know how to install the thing.  There are a lot of similarities to working with solution packages but there are several differences to be aware of.  The documentation on TechNet is pretty good, but putting it all together can be tricky.

Specific to PowerShell, there is some terminology to familiarize yourself with.  You’ll see these for parameter names so you need to know what they are otherwise you’ll find yourself confused.

  • App Package – physical file containing the app (.app file)
  • App – an instance of an app installed on a particular subsite

Along with the App, you’ll find an Id property that refers to a GUID of that particular app instance.  We’ll talk about that more when it comes to updates.

To begin installing app, we first install the .app file into a site collection and then we deploy the app to an individual subsite.  To install the app package, we use Import-SPAppPackage.  Specify the path to your app package (.app file) with the –Path parameter.  You’ll also need to specify which Site Collection will contain the app using the –Site parameter.  Finally, the Source parameter tells SharePoint where the app came from (SharePoint Store, Corporate Catalog, or Object Model).  I always specify a value of ObjectModel for my custom apps.  Here’s what the command looks like.  Assign the results of the command to variable so that you can use it in the next step.

$spapp = Import-SPAppPackage -Path .\spcdemoapp.app -Site http://server/sitecollection –Source ObjectModel

PowerShellAppImportSPAppPackage

You can always add –confirm:$false to avoid getting prompted.

Now you have the AppPackage installed, you can deploy an instance of it to a subsite using Install-SPApp.  You’ll need to provide a URL to the subsite using the –Web parameter and a reference to the imported app package.  That’s why we saved the results of the last command into $spapp.  Use the following command.

$instance = Install-SPApp -Web http://server/sitecollection/site -Identity $spapp

PowerShellAppInstallApp

Like always with PowerShell, no news is good news.  At this point, you should find the app installed on the Site Contents pages.

NewAppInstalled

Here’s what it looks like it is running.  This is mainly for reference when we start talking about updates.

NewAppDefaultPage

To determine what apps are installed on a particular subsite use Get-SPAppInstance.  This cmdlet can be executed three different ways.  You need the id of the App Instance to update, export, or remove it.   So often you combine it with a Where-Object command to get a reference to an app instance by name instead of by id.

$instance = Get-SPAppInstance -web http://server/sitecollection/site | Where-Object { $_.Title -eq "MyApp" }

PowerShellAppGetSPAppInstance

Once you have a reference to the App Instance, removing the app from a subsite is easy with Uninstall-SPAppInstance.

Uninstall-SPAppInstance -Identity $instance

PowerShellAppUninstallSPAppInstance

One nice feature of PowerShell is that you have the ability to export an installed app to a .app file using Export-SPAppPackage.  It takes a parameter named -App.  It expects the value from an instance called .App (so use Get-SPAppInstance as shown above).  You also want to specify where to save the file using the –Path parameter.

Export-SPAppPackage –App $instance.App –Path .\ExportApp.app

PowerShellAppExportSPAppPackage

When it comes to updating apps, that’s where things get a bit tricky.  Here is what you need to do:

  • Get a reference to the existing installed instance using Get-SPAppInstance shown earlier
  • Import the new app package with Import-SPAppPackage
  • Use Update-SPAppInstance with a reference to the imported app package and the existing instance

Here are the commands

$instance = Get-SPAppInstance -web http://server/sitecollection/site | Where-Object { $_.Title -eq "MyApp" }

$spappv2 = Import-SPAppPackage -Path .\myapp.app -Site http://server/sitecollection/site -Source ObjectModel

Update-SPAppInstance – Identity $instance –App $spappv2

PowerShellAppUpdateSPAppInstance

One thing you might have noticed by now is that there is no way to retrieve which app packages are installed nor is there any way to remove them using PowerShell.  This is a bit different than the way we deal with solution packages.  I suspect it cleans itself up, but I’m really not sure.  I hope these PowerShell commands prove to be useful to you.  Let me know if you run into any issues.

Comments

# re: Installing SharePoint 2013 apps with PowerShell

Friday, March 1, 2013 3:03 AM by Shilpa

Nice article.

# Installing SharePoint 2013 Apps | Sladescross's Blog

Monday, March 4, 2013 1:08 AM by Installing SharePoint 2013 Apps | Sladescross's Blog

Pingback from  Installing SharePoint 2013 Apps | Sladescross's Blog

# Installing SharePoint 2013 apps with PowerShell - Corey Roth [MVP] | SharePoint Resources | Scoop.it

Pingback from  Installing SharePoint 2013 apps with PowerShell - Corey Roth [MVP] | SharePoint Resources | Scoop.it

# re: Installing SharePoint 2013 apps with PowerShell

Tuesday, March 12, 2013 12:42 PM by Tim Vivian

Great Article Corey.  Very useful information that I can use in my own automation strategy.

# re: Installing SharePoint 2013 apps with PowerShell

Tuesday, August 6, 2013 11:46 PM by VIvek

Can we add APPS to Pages programatically? I have a sharepoint hosted app which i have to place on my subsite as soon as the site gets created. This is a functionality i need as part of my site Provisioning. Can you please help me figure out if this task is achievable using APP model?

# re: Installing SharePoint 2013 apps with PowerShell

Tuesday, October 22, 2013 4:56 AM by Juan

If I try and uninstall some apps I get the following error:

"Uninstall-SPAppInstance : The App package has not been downloaded."

# re: Installing SharePoint 2013 apps with PowerShell

Thursday, December 19, 2013 7:56 AM by Robert

I get an invalid data error when trying to uninstall an app.  This is at the root site collection of my web application.  What am I missing?

# re: Installing SharePoint 2013 apps with PowerShell

Monday, September 22, 2014 3:57 PM by Greg Thatcher

Great post.  Thanks for the info on updating -- haven't seen this explained correctly anywhere else.

# re: Installing SharePoint 2013 apps with PowerShell

Tuesday, September 30, 2014 10:30 AM by Srikanth

Hi Corey,

I uploaded an app to my app catalog. Is there a way to deploy this app to my other site collection programatically?

# re: Installing SharePoint 2013 apps with PowerShell

Wednesday, October 7, 2015 2:13 PM by Phil

I notice you're installing to a developer site. Can PowerShell only be used to manage apps on sites with "sideloading" enabled?

# re: Installing SharePoint 2013 apps with PowerShell

Friday, October 16, 2015 8:56 PM by Rajesh

Hi Corey - All the powershell commands in this blog applies to On-premise SharePoint APP installation i believe. How can we Register/Install App in a SharePoint Online site ?

Thanks

# re: Installing SharePoint 2013 apps with PowerShell

Thursday, October 6, 2016 9:40 AM by Kai

Hello I am getting an error whith the updating function. It says "the provided app differs from another app with the same version and product id" for the Import-Part in the Updateing solution because the app is already installed. Can you assist here?

Leave a Comment

(required)
(required)
(optional)
(required)