Skip to content

Instantly share code, notes, and snippets.

@YoraiLevi
Last active October 30, 2024 17:15
Show Gist options
  • Save YoraiLevi/e1888ee1c06b34cb02d4b58b739301af to your computer and use it in GitHub Desktop.
Save YoraiLevi/e1888ee1c06b34cb02d4b58b739301af 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://www.microsoft.com/p/dynamic-theme/9nblggh1zbkw" "$ENV:USERPROFILE\Desktop"
# C:\Users\user\Desktop\55888ChristopheLavalle.DynamicTheme_1.4.30233.0_neutral_~_jdggxwd41xcr0.AppxBundle
# C:\Users\user\Desktop\55888ChristopheLavalle.DynamicTheme_1.4.30234.0_neutral_~_jdggxwd41xcr0.AppxBundle
# C:\Users\user\Desktop\Microsoft.NET.Native.Framework.1.7_1.7.27413.0_x64__8wekyb3d8bbwe.Appx
# C:\Users\user\Desktop\Microsoft.NET.Native.Runtime.1.7_1.7.27422.0_x64__8wekyb3d8bbwe.Appx
# C:\Users\user\Desktop\Microsoft.Services.Store.Engagement_10.0.19011.0_x64__8wekyb3d8bbwe.Appx
# C:\Users\user\Desktop\Microsoft.VCLibs.140.00_14.0.29231.0_x64__8wekyb3d8bbwe.Appx
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*'} | 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