Using a worklet to remove Skype Appx

Attempting to check for the presence of the Microsoft Store App version of Skype. If present, run the Remediation (remove). The Worklet displays an Error status in the Activity Log, but I do not know why, nor how to check the logs/console.

Using a modified version of the Skype install script - it does the same 32/64 bit checks, but remediates if Skype was found, rather than not found. At least I think.

Eval:

#REQUIRES -Version 2.0
<#
.SYNOPSIS
    This Test script checks to see if Skype is installed
.DESCRIPTION
    This script queries the installed files for 32 and 64bit software
    and returns a 1 if product is installed or 0 if not
.Notes
    File Name       :Skype_Uninstall_Test.ps1
    Author          :
    Prerequisite    :PowerShell V2 over win7 and upper
#>
#Handle Exit Codes:
trap {  $host.ui.WriteErrorLine($_.Exception); exit 90 }

function Skype_Uninstall_Test {
    <#
    .SYNOPSIS
        This function Checks a Skype installation.
    .DESCRIPTION
        After checking the Skype availability, based on the exit code tool will decide to call remediation code or not.
    .EXAMPLE
        Skype_Uninstall_Test
    .NOTES
    #>

    # Application Name which is to be installation verified.
    $appName = 'Skype'
    # Finding the Systen Directory (system32 Directory).
    $sysDir = [Environment]::SystemDirectory

    # Making the 32 bit and 64 bit path names blank.
    $32BIT = ""
    $64BIT = ""

    #Get Registry value for the 32 bit software installed on both 32-bit and 64-bit machine
    Try {  $32BIT = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' -ErrorAction Stop |
           Select-Object DisplayName |
           Select-String "$appName" |
           Out-String
    }
    Catch { $Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber;
            Write-Output $Exception
            exit 90
    }
    # Script blcok to execute with powershell
    # Match name in the block should be hard coded.
    $scriptBlock = {$key = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' |
                        Where-Object { $_.DisplayName -match 'Skype'}
                        return $key
                    }

    # Get the Registry value for the 64 bit software installed on the 64 bit machine. as the automox process is 32 bit
    if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "64-bit")
    {
        # Call for accessing the 64 bit registry incase 32 bit process not access the registry.        
		Try
		{
			$installed64 = @(& "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptBlock)
			$64BIT = $installed64.DisplayName
		}
		Catch
		{
			$Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber;
			Write-Output $Exception
			exit 90
		}
        
    } elseif ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "32-bit") {
        # Checking for the 64 bit registry on 32 bit host.
        # In Script block line 38 match name should be hard coded and it should be proper.
        Try {
            $installed64 = @(& "$sysDir\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptBlock)
            $64BIT = $installed64.DisplayName
        }
        Catch { $Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber
                Write-Output $Exception
                exit 90
        }
    }

    # Check for the Availability of the software and exit with relevent exit code.
    if ($32BIT.Trim() -match $appName -or ($64BIT -ne $null -and $64BIT.Trim() -match $appName)) {
        #Application Found, Run remediation script to uninstall it!
        exit 1
    } else {
        # Application is not installed. Exit
        exit 0
    }
}

Skype_Uninstall_Test

Remediation:

#REQUIRES -Version 2.0
<#
.SYNOPSIS
    This script allows an admin to uninstall Skype
.DESCRIPTION
    This script uninstalls the Skype App for all users
.NOTES
    File Name       :Skype_Unnstall.ps1
    Author          :
    Prerequisite    :PowerShell V2 over win7 and upper
#>

#Handle Exit Codes:
trap {  $host.ui.WriteErrorLine($_.Exception); exit 90 }

function Skype_Uninstall_Rem {

    <#
    .SYNOPSIS
        This function allows automox to Uninstall Skype on Windows .
    .DESCRIPTION
        This function uninstalls Skype on system.
    .EXAMPLE
        Skype_Uninstall_Rem
    .NOTES
    #>

	$AppList = "Microsoft.SkypeApp"
 
	ForEach ($App in $AppList)
	{
	 
		$PackageFullName = (Get-AppxPackage $App).PackageFullName
		$ProPackageFullName = (Get-AppxProvisionedPackage -online | where {$_.Displayname -eq $App}).PackageName
	 
		if ($PackageFullName){
			Write-Output "Removing Package: $App"
			try {
				Write-Output "Removing Package: $App"
				remove-AppxPackage -package $PackageFullName -AllUsers -ErrorAction Stop | Out-Null
			} 
			catch {
				Write-Output "ERROR: $_"
					exit 90
			}
			
		}
		else {
			Write-Output "WARNING: Unable to find package: $App"
		}
	 
		if ($ProPackageFullName){
			Write-Output "Removing Provisioned Package: $ProPackageFullName"
			try {
				Remove-AppxProvisionedPackage -online -packagename $ProPackageFullName -AllUsers -ErrorAction Stop | Out-Null
			}
			catch {
				Write-Output "ERROR: $_"
				exit 90
			}
		}
		else{
			Write-Output "WARNING: Unable to find provisioned package: $App"
				exit 90
		}
	 
	}
}

Skype_Uninstall_Rem
1 Like

Heck yeah!

The 32/64 bit reg checks are annoying to work through the first time, but now you’ve got a great template for future detection Worklets.

Side note: I always find it funny that Microsoft started PowerShell with the v1.0 directory structure planning to increment as time went on, but they didn’t think through the fact that it would break everyone’s automation every time they altered the path. Just a little tidbit if anyone ever wonders why their modern PowerShell path still says 1.0.