简体   繁体   中英

Powershell is running at startup but not executing Remove-AppxPackage

Executing a Powershell script at startup to strip out some of the bloat which comes with Windows 10. It includes a check to see if it has already executed, which creates a flag file (so I know the script is running). However, the uninstall commands are not running (they run fine manually).

Hosts are running Windows 10 Pro. Script is being delivered via GPO; being applied ok as evidenced by the script running.

$DestinationFile = "C:\BloatFlag.txt"
if (Test-Path $DestinationFile) {

echo "Already Done"

} else {

Get-AppXPackage Microsoft.Microsoft3DViewer | Remove-AppXPackage
Get-AppXPackage DellInc.DellSupportAssistforPCs | Remove-AppXPackage
Get-AppXPackage DellInc.DellCommandUpdate | Remove-AppXPackage
# and the list goes on

echo "ONLY DELETE THIS FILE IF YOU REQUIRE TO RUN THE BLOAT REMOVAL SCRIPT AGAIN" > "C:\BloatFlag.txt"

}
  • BloatFlag.txt is created when checking C:\\ .
  • None of the listed software has been removed

I expected the apps would have been removed if the code reached creation of the text file.

Maybe. Maybe not. Here is what I mean...

a) If Get-AppXPackage doesn't return anything, Remove-AppXPackage won't delete anything, and the marker file will still be created.

b) If either statement throws an exception, from what you've coded, that won't stop script execution and the marker file will be created. For example, stick the following in a file and run it. You'll see Remove-AppXPackage throw an exception, but thinger.txt still gets created.

Remove-AppXPackage xyz
write-output 'test' > thinger.txt

When the script executes during the boot/login/whatever process, does it run as the user who's app-packages are being removed, or does it run under a different user's context? If the latter, the user needs to be an admin (running under elevated permissions) to delete app-packages from another user's profile. if you're not sure, add a whoami to the output in the marker file you're creating.

Just a side note, I vaguely recall people Remove-AppxPackage and Remove-ProvisionedAppxPackage . The first removed the package for a given local user. The second removes the package from the machine, so new users won't have the package installed. It wouldn't surprise me if you needed to run Remove-ProvisionedAppxPackage with elevated permissions, but I don't see that in the cmdlet's documentation.

Remove-Appxpackage is largely disabled these days, unless you want to do some sqlite database manipulation. https://superuser.com/questions/1115801/unable-to-uninstall-universal-apps-through-powershell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM