If statement not working as intended in powershell script
14:12 27 Mar 2026

The code below is to debloat workstations. It works, but the if statement does not work as intended. It removes the app installed, but writes removal failed instead of removal successful.

# Loop through the list and remove each app
foreach ($AppName in $AppsToRemove) {
    Write-Host "Attempting to remove $AppName..."
    
    # Get the package for the current user and pipe it to Remove-AppxPackage
     if ((Get-AppxPackage -Name $AppName | Remove-AppxPackage -AllUsers))
    {
      #Remove Provisioned package
      Get-AppxProvisionedPackage -Online | Where-Object {$_.DisplayName -like $AppName} | Remove-AppxProvisionedPackage -Online
      Write-Host "Removal Successful"
    }
      else
    {
      Write-Host "Removal Failed"
    }

}
powershell if-statement debugging