# Remove unwanted provisioning package that are installed every time a new user is created or windows is updated

# Remove our own old ppkg
echo "Removing old ppkg"
Get-ProvisioningPackage | Where-Object {$_.PackageName -like "M6TABHP*"} | ForEach-Object { Remove-ProvisioningPackage -Path $_.PackagePath }

# Get-AppxProvisionedPackage -online | select-object DisplayName
# List packages with:

echo "Removing Amazon"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -eq "Amazon.com.Amazon"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

echo "Removing Skype"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -eq "Microsoft.SkypeApp"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

echo "Removing Netflix"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -like "*.Netflix"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

echo "Removing Office"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -like "Microsoft.Office.*"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

echo "Removing Weather"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -eq "Microsoft.BingWeather"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

echo "Removing Jump Start"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -like "*.HPJumpStart"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

echo "Removing Solitaire"
Get-AppxProvisionedPackage -online | where-object {$_.DisplayName -like "Microsoft.MicrosoftSolitaireCollection"} | ForEach-Object { Remove-AppxProvisionedPackage -online -PackageName $_.PackageName}

# Remove installed apps
# List them with:
# Get-AppxPackage | select Name

echo "Removing applets"
Get-AppxPackage -Name "Microsoft.SkypeApp" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.BingNews" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.BingWeather" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.MicrosoftSolitaireCollection" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.BingSports" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.BingFinance" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.HPJumpStart" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.MicrosoftOfficeHub" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.Twitter" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.Office.*" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.Getstarted" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Amazon.com.Amazon" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.Netflix" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.MarchofEmpires" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Microsoft.MinecraftUWP" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "OxfordUniversityPressh.*" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "Facebook.Facebook" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.DisneyMagicKingdoms" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.HiddenCityMysteryofShadows" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "king.com.CandyCrushSodaSaga" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "king.com.CandyCrushSaga" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "king.com.BubbleWitch3Saga" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "king.com.*" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "*.DragonManiaLegends" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}
Get-AppxPackage -Name "NORDCURRENT.COOKINGFEVER" | ForEach-Object { Remove-AppxPackage -Package $_.PackageFullName}

# Remove applications
# List them with:
# Get-WmiObject -Class Win32_Product | Select-Object Name

# Ask for confirmation... Not good for unattended script.
echo "Removing HP Support Assistant"
$app = Get-WmiObject -Class Win32_Product -Filter "Name = 'HP Support Assistant'"
if ($app) {$app.Uninstall()}

#echo "Removing HP JumpStart Launch"
#$app = Get-WmiObject -Class Win32_Product -Filter "Name = 'HP JumpStart Launch'"
#if ($app) {$app.Uninstall()}

#echo "Removing HP JumpStart Bridge"
#$app = Get-WmiObject -Class Win32_Product -Filter "Name = 'HP JumpStart Bridge'"
#if ($app) {$app.Uninstall()}

echo "Removing HP Registration Service"
$app = Get-WmiObject -Class Win32_Product -Filter "Name = 'HP Registration Service'"
if ($app) {$app.Uninstall()}

# List applications with
# Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, Publisher | Format-Table -AutoSize

function uninstallForPosapp($appName)
{
  echo "Remove $appName"
  $uninstall = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object -Property DisplayName -Like -Value $appName
  if (!$uninstall) {
    return
  }
  if (Get-Member -inputobject $uninstall -name "QuietUninstallString" -Membertype Properties) {
    if (Test-Path $uninstall.InstallLocation.Trim('"') -PathType Container) {
      $uninstall = $uninstall.QuietUninstallString
      Write "Uninstalling..."
      cmd.exe /c "$uninstall"
	}
	return
  }
  if (Get-Member -inputobject $uninstall -name "UninstallString" -Membertype Properties) {
    $uninstall = $uninstall.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall = $uninstall.Trim()
	if ($uninstall) {
      Write "Uninstalling..."
      start-process "msiexec.exe" -arg "/X $uninstall /qb" -Wait
	  return
	}
  }
}

#uninstallForPosapp("HP Support Assistant")
uninstallForPosapp("HP School Pack Installer")
uninstallForPosapp("HP JumpStart Launch")
uninstallForPosapp("HP JumpStart Bridge")
#uninstallForPosapp("HP Registration Service")

echo "Remove OneDrive"
if (Test-Path "$env:systemroot\System32\OneDriveSetup.exe") {
    & "$env:systemroot\System32\OneDriveSetup.exe" /uninstall
}
if (Test-Path "$env:systemroot\SysWOW64\OneDriveSetup.exe") {
    & "$env:systemroot\SysWOW64\OneDriveSetup.exe" /uninstall
}
