- Context
I’ve discovered recently this link to reinstall-preinstalledApps.zip that was provided on this page
- Solution
The code is straightforward and probably does the job in most situations.
But, there are some assumptions about the version, admin rights,.. and the code has no error handling. You can see the file here.
I’ve decided to improve it by adding a help, a #require statement, error handling and the ability to use the -WhatIf parameter.
If you use my “slightly improved” version, you can do:
# Show what would be done using the -WhatIf parameter Start-ReInstallAppx -Filter "*Microsoft.WindowsStore*" -WhatIf # Do it: Start-ReInstallAppx -Filter "*Microsoft.WindowsStore*" -Verbose
#Requires -RunAsAdministrator | |
Function Start-ReInstallAppx { | |
<# | |
.SYNOPSIS | |
Get back all the apps that come default with Windows 10 | |
.DESCRIPTION | |
Get back all the apps that come default with Windows 10 | |
.EXAMPLE | |
Start-ReInstallAppx -Filter "*Microsoft.WindowsStore*" | |
.NOTES | |
http://go.microsoft.com/fwlink/?LinkId=619547 | |
Adapted from | |
http://download.microsoft.com/download/5/F/0/5F04003A-035E-4A0F-9662-43E32C546F6C/reinstall-preinstalledApps.zip | |
#> | |
[CmdletBinding(SupportsShouldProcess)] | |
Param( | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string]$Filter | |
) | |
Begin { | |
$HT = @{ ErrorAction = 'Stop'} | |
} | |
Process { | |
try { | |
# Get all the provisioned packages | |
$Packages = Get-Item -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\Applications' @HT | | |
Get-ChildItem @HT | |
} catch { | |
Write-Warning -Message "Failed to get packages list from the registry or filesystem because $($_.Exception.Message)" | |
} | |
if ($Packages) { | |
if ($Filter) { | |
$Packages = $Packages | Where-Object {$_.Name -like "$($Filter)" } | |
} | |
$Packages | | |
ForEach-Object -Process { | |
$p = $_ | |
$PackageName = $PackagePath = $null | |
# get package name & path | |
try { | |
$d = $p | Get-ItemProperty @HT | |
if ($d) { | |
$PackageName = $d | Select-Object -ExpandProperty PSChildName | |
$PackagePath = [System.Environment]::ExpandEnvironmentVariables(($d| Select-Object -ExpandProperty Path)) | |
} | |
} catch { | |
Write-Warning -Message "Failed to get package name and path for package $($p) because $($_.Exception.Message)" | |
} | |
# register the package | |
if ($PackageName -and $PackagePath) { | |
Write-Verbose -Message "Attempting to register package: $($PackageName)" | |
if ($PSCmdlet.ShouldProcess("$($PackageName))",'Register Appx package')) { | |
try { | |
Add-AppxPackage -Register $PackagePath -DisableDevelopmentMode @HT | |
Write-Verbose -Message "Successfully registered package from path $($PackagePath)" | |
} catch { | |
Write-Warning -Message "Failed to regiter from $($PackagePath) because $($_.Exception.Message)" | |
} | |
} | |
} | |
} | |
} | |
} | |
End {} | |
} |