0

Update

I found the answer. The below code is working for me.

Function AppCheck {
    #Check for Calculator app
    $CalcCheck = $null
    $CalcCheck = Get-AppxPackage -Name "*Calculator*"
    #Check for Photos app
    $PhotoCheck = $null
    $PhotoCheck =  = Get-AppxPackage -Name "*Photos*"
    #Check for OneDrive app
    $OneCheck = $null
    $OneCheck =  = Get-AppxPackage -Name "*OneNote*"
    #Check for Windows Store app
    $StoreCheck = $null
    $StoreCheck = Get-AppxPackage -Name "*Store*"
    if ($null -eq $CalcCheck -or $null -eq $PhotoCheck -or $null -eq $OneCheck -or $null -eq $StoreCheck){
        Write-Host "Default Windows apps not installed. Reinstalling now."
        $DefaultApps = Get-AppXPackage -AllUsers
        foreach ($DefaultApp in $DefaultApps.InstallLocation){
            Add-AppxPackage -DisableDevelopmentMode -Register "$DefaultApp\AppXManifest.xml"
        }
    }

Original Question

I have a script with a function that is supposed to reinstall all Windows 10 default apps if certain apps aren't detected. It will execute all of the code correctly, except it throws errors when reinstalling Windows 10 apps. This code runs as admin.

When run in PowerShell as admin manually, the below code works fine:

Get-AppXPackage | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml”}

However, when ran in a script with my function, I get errors like this for every single app it tries to reinstall:

Add-AppxPackage : The URI specified is invalid:
“C:\Windows\SystemApps\Microsoft.Windows.CloudExperienceHost_cw5n1h2txyewy\AppXManifest.xmlâ€
At C:\Users\Test2\Desktop\Uinta-NewUser.ps1:48 char:36
+ ...  | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register “$($_ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-AppxPackage], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.Windows.Appx.PackageManager.Commands.AddAppxPackageComm
   and

I know my If statement works because my Write-Output command works every single time correctly. My full function is below. I'd appreciate any help on this.

Full function:

Function AppCheck {
    #Check for Calculator app
    $CalcCheck = $null
    $CalcCheck = Get-AppxPackage -Name "*Calculator*"
    #Check for Photos app
    $PhotoCheck = $null
    $PhotoCheck =  = Get-AppxPackage -Name "*Photos*"
    #Check for OneDrive app
    $OneCheck = $null
    $OneCheck =  = Get-AppxPackage -Name "*OneNote*"
    #Check for Windows Store app
    $StoreCheck = $null
    $StoreCheck = Get-AppxPackage -Name "*Store*"
    
    #Check if essential apps are missing. If so, install all Windows 10 default apps.
    if ($null -eq $CalcCheck -or $null -eq $PhotoCheck -or $null -eq $OneCheck -or $null -eq $StoreCheck){
        Write-Output "Default Windows apps not installed. Reinstalling now."
        Get-AppXPackage | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml”}
        #Get-Job | Wait-Job
    }
}
JB.Uinta
  • 1
  • 2
  • 1
    While ASCII-range double quotes (`"`) are the safer choice, note that use of non-ASCII-range quotation marks such as `“` (LEFT DOUBLE QUOTATION MARK, [`U+201C`](http://www.fileformat.info/info/unicode/char/201c)) isn't a problem _per se_ - PowerShell recognizes them _interchangeably_ with their ASCII-range counterparts. The real problem here is one of _character encoding_: The script is UTF-8-encoded, but lacks a BOM, which causes Windows PowerShell to misinterpret it as ANSI-encoded; see [this answer](https://stackoverflow.com/a/55053609/45375). /cc @zett42 – mklement0 Nov 09 '21 at 23:05

0 Answers0