0

I want to make PowerShell script that removes pre-installed app and I have list of those apps.

The problem is, I want to make script to run certain command first, and only if the command failed, run another command.

Like, if script failed to remove app, then it will try to disable that app. No need to try disabling app that already removed, right?

adb shell am force-stop <app> # force stop app
adb shell am kill <app> # kill app if not force-stopped

adb uninstall <app> # remove app
adb shell pm disable-user --user 0 <app> # disable app for user 0 if not removed

Yes, there are some already-answered, similar question like this. But those are not working right now.

PS > adb shell "pm uninstall com.google.android.apps.photos; echo $?"
Failure [DELETE_FAILED_INTERNAL_ERROR]
True

Even if the command failed(Failure), echo returns True.

This is tested with latest(just downloaded few minutes ago) ADB and Android 11, so my information('that doesn't working!') is more 'latest' I guess.

So, how to use return code of ADB command in PowerShell?

PS. If you want what I've coded for more inspection, here it is: LINK
It has been modified not to use echo, but still no success.

tetratheta
  • 177
  • 1
  • 13

1 Answers1

0

So, how to use return code of ADB command in PowerShell?

Correct way to check this should be:

adb devices
if($?) {
  echo "Command succedded"
} else {
  echo "Command failed!"
}

Moreover, there's another exit code $LASTEXITCODE which indicates 0, if command executed without any exception; and non-zero value, if command failed with some exception. You can use this as:

if($LASTEXITCODE -eq 0) {
  echo "Command succedded"
} else {
  echo "Command failed!"
}

The difference between $? and $LASTEXICODE is that the former returns Boolean, while the latter returns an Int32 type.

Even if the command failed(Failure), echo returns True.

The real problem why you are facing this issue is, because you are trying to uninstall the Google Photos application, which is a system app pre-bundled along with other OEM vendor apps, with minimal version. To uninstall such system apps you need root permission which can be activated using --user 0 flag to adb pm command but you need root access. When you purchase a fresh device, it's already installed as system app and can be updated. When you try to uninstall it, its updates will be uninstalled and the system restores the minimal pre-bundled version rather than completely uninstalling. That's why you are getting the Failure [DELETE_FAILED_INTERNAL_ERROR] message and subsequently getting True, which should be False but the command doesn't throw that. Weird.

Jay
  • 2,852
  • 1
  • 15
  • 28
  • Yes, that's why `adb shell pm disable-user --user 0 ` tries to 'disable' it. I've found even though user cannot disable those 'OEM vendor apps' in Settings, but still can disable it via ADB. and even if those commands fail, I can also 'uninstall' the app for user 0. // and yes, I don't know why ADB returns `True` when the command failed. Maybe I should use library stuff, rather than just using ADB executable... – tetratheta Aug 16 '21 at 08:52