Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cregx/7c54c212681bc0d1a50d018f0a82a3e3 to your computer and use it in GitHub Desktop.
Save cregx/7c54c212681bc0d1a50d018f0a82a3e3 to your computer and use it in GitHub Desktop.
How to install an .Appx or .AppxBundle via a PowerShell script

How to Install an .Appx or .AppxBundle via a PowerShell script

Introduction

This how-to uses a PowerShell script template to demonstrate how to automatically install an AppxBundle in the Windows (e.g. 10) environment.

All the necessary information can be found in the inline comments within the script. To use the script for your own purposes, only a few adjustments are required.

PowerShell script

Code sections in the script that need to be adjusted. Replace the sample filenames <...> inside the array with the correct filenames from the appx.

# Set AppX application display name for script outputs.
$appxName = "My Application"
...

# Define an array with all files which are needed for the installation of the appx.
$files = @(
    ("<Application file name>.AppxBundle"),
    ("<Dependencies1 file name>.Appx"),
    ("<Dependencies2 file name>.Appx"),
    ("<File name of the license file>.xml")
)

Disclaimer

This manual or parts of it are provided "as is" without warranty of any kind.

MIT License

Copyright (c) 2022 cregx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# This script installs an application bundle (AppX).
# Set AppX application display name for script outputs.
$appxName = "My Application"
Write-Output "Installation of AppX Bundle application '$appxName'."
# Define an array with all files which are needed for the installation of the appx.
$files = @(
("<Application file name>.AppxBundle"),
("<Dependencies1 file name>.Appx"),
("<Dependencies2 file name>.Appx"),
("<File name of the license file>.xml")
)
# Determine your own execution path.
$myPath = $MyInvocation.MyCommand.Definition
$sources = Split-Path $myPath -Parent
# Identify individual files from the files array and prepare them for invoke-expression use.
$appxFile = ""
$appxLicenseFile = ""
$appxDependencyFiles = ""
Write-Output "Bundle files used..."
$counter = 0
$tmp = ""
foreach ($fileX in $files){
Write-Output $fileX
# Dependency files: Determine dependency files and prepare them comma-separated for use in the -DependencyPackagePath parameter.
if ($fileX -match "Appx$"){
if ($counter -gt 0){
$tmp = Join-Path -Path $sources $fileX
$appxDependencyFiles += ', ' + $tmp
}
else {
$appxDependencyFiles += Join-Path -Path $sources $fileX
}
$counter++
}
# AppX file for the -PackagePath parameter.
if ($fileX -match "AppxBundle$"){
$appxFile = Join-Path -Path $sources $fileX
}
# License file for the -LicensePath parameter.
if ($fileX -match "xml$"){
$appxLicenseFile = Join-Path -Path $sources $fileX
}
# Include other dependencies here if necessary, e.g. for -OptionalPackagePath.
# Important: The $params must be extended accordingly.
}
# Compose invoke command for the installation.
# See also: https://docs.microsoft.com/en-us/powershell/module/dism/add-appxprovisionedpackage?view=windowsserver2022-ps
# For logs (without -LogPath parameter) see under the default file path: %WINDIR%\Logs\Dism\dism.log
$params = "Add-AppxProvisionedPackage -PackagePath " + $appxFile + " -DependencyPackagePath " + $appxDependencyFiles + " -LicensePath " + $appxLicenseFile + " -Online"
# Install the bundle.
Invoke-Expression $params
# Check and output the result.
if ($? -eq $true) {
Write-Output "Successfully installed."
} else {
Write-Output "Installation failed."
}
Write-Output "For logs see under default file path: %WINDIR%\Logs\Dism\dism.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment