Skip to content

Instantly share code, notes, and snippets.

@winadm
Forked from YoraiLevi/Download-AppxPackage.ps1
Last active February 19, 2024 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winadm/1c069fa7a724b6f31b82b973f08d4022 to your computer and use it in GitHub Desktop.
Save winadm/1c069fa7a724b6f31b82b973f08d4022 to your computer and use it in GitHub Desktop.
Download Windows Store Apps with Powershell
#https://serverfault.com/questions/1018220/how-do-i-install-an-app-from-windows-store-using-powershell
#Usage:
# Download-AppxPackage "https://apps.microsoft.com/detail/9NKSQGP7F2NH" "$ENV:USERPROFILE\Desktop"
# download WhatsApp
function Download-AppxPackage {
[CmdletBinding()]
param (
[string]$Uri,
[string]$Path = "."
)
process {
$Path = (Resolve-Path $Path).Path
#Get Urls to download
$WebResponse = Invoke-WebRequest -UseBasicParsing -Method 'POST' -Uri 'https://store.rg-adguard.net/api/GetFiles' -Body "type=url&url=$Uri&ring=Retail" -ContentType 'application/x-www-form-urlencoded'
$LinksMatch = $WebResponse.Links | where {$_ -like '*.appx*' -or $_ -like '*.appxbundle*' -or $_ -like '*.msix*' -or $_ -like '*.msixbundle*'} | where {$_ -like '*_neutral_*' -or $_ -like "*_"+$env:PROCESSOR_ARCHITECTURE.Replace("AMD","X").Replace("IA","X")+"_*"} | Select-String -Pattern '(?<=a href=").+(?=" r)'
$DownloadLinks = $LinksMatch.matches.value
function Resolve-NameConflict{
#Accepts Path to a FILE and changes it so there are no name conflicts
param(
[string]$Path
)
$newPath = $Path
if(Test-Path $Path){
$i = 0;
$item = (Get-Item $Path)
while(Test-Path $newPath){
$i += 1;
$newPath = Join-Path $item.DirectoryName ($item.BaseName+"($i)"+$item.Extension)
}
}
return $newPath
}
#Download Urls
foreach($url in $DownloadLinks){
$FileRequest = Invoke-WebRequest -Uri $url -UseBasicParsing #-Method Head
$FileName = ($FileRequest.Headers["Content-Disposition"] | Select-String -Pattern '(?<=filename=).+').matches.value
$FilePath = Join-Path $Path $FileName; $FilePath = Resolve-NameConflict($FilePath)
[System.IO.File]::WriteAllBytes($FilePath, $FileRequest.content)
echo $FilePath
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment