#
# Uninstall-AppAndService.ps1
#
# MUST be run as an administrator
# MUST have PS 5+ or your mileage will vary-considerably
#
#####################################################################
#
# Examples:
#
# Uninstall an application only:
#
# .\Uninstall-AppAndService.ps1 -ApplicationName 'Your Application Name'
#
# Uninstall a service only
#
# .\Uninstall-AppAndService.ps1 -ServiceName 'YourServiceName'
#
# NOTE: -ServiceName uses the short version, as in:
#
# $svcName = "SomeAppxService" ...not the longer name.
# Uninstall both:
#
# .\Uninstall-AppAndService.ps1 -ApplicationName 'Your Application Name' -ServiceName 'YourServiceName'
#
# Targetting multiple computers:
#
# 1. define the array:
# $computers = @('Computer1', 'Computer2', 'Computer3')
#
# 2. use that array in the arguments:
# .\Uninstall-AppAndService.ps1 -ApplicationName 'Your Application Name' -ServiceName 'YourServiceName' -ComputerNames $computers
#
#######################################################################
#
# Define parameters
#
param (
[Parameter(Mandatory = $true)]
[string]$ApplicationName,
[Parameter(Mandatory = $false)]
[string]$ServiceName,
[Parameter(Mandatory = $false)]
[string[]]$ComputerNames = @('localhost')
)
#
#######################################################################
#
# Function to check for administrative privileges
#
function Test-AdminRights {
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Output "This script requires administrative privileges. Please run it as an administrator."
exit
} else {
Write-Host "Running with administrative privileges."
}
}
#
#######################################################################
#
# Function to check PowerShell version
#
function Test-PowerShellVersion {
$PSVersion = $PSVersionTable.PSVersion
Write-Host "Current PowerShell version: $PSVersion"
if ($PSVersion.Major -lt 5) {
Write-Output "PowerShell version 5.0 or higher is recommended for this script."
}
}
#
#######################################################################
#
# Function to enable PowerShell Remoting on the local machine
#
function Enable-LocalPSRemoting {
Write-Host "Enabling PowerShell Remoting on the local machine..."
Enable-PSRemoting -Force
Write-Host "PowerShell Remoting has been enabled."
}
#
#######################################################################
#
# Function to uninstall application
#
function Uninstall-Application {
param (
[string]$AppName,
[string]$Computer
)
Invoke-Command -ComputerName $Computer -ScriptBlock {
param ($AppName)
$app = Get-CimInstance -ClassName Win32_Product | Where-Object { $_.Name -eq $AppName }
if ($app) {
Invoke-CimMethod -InputObject $app -MethodName Uninstall | Out-Null
Write-Output "Application '$AppName' uninstalled successfully on $env:COMPUTERNAME."
} else {
Write-Output "Application '$AppName' not found on $env:COMPUTERNAME."
}
} -ArgumentList $AppName
}
#
#######################################################################
#
# Function to remove service
#
function Remove-Service {
param (
[string]$SvcName,
[string]$Computer
)
Invoke-Command -ComputerName $Computer -ScriptBlock {
param ($SvcName)
$service = Get-Service -Name $SvcName -ErrorAction SilentlyContinue
if ($service) {
if ($service.Status -eq 'Running') {
Stop-Service -Name $SvcName -Force
}
sc.exe delete $SvcName
Write-Output "Service '$SvcName' removed successfully on $env:COMPUTERNAME."
} else {
Write-Output "Service '$SvcName' not found on $env:COMPUTERNAME."
}
} -ArgumentList $SvcName
}
#
#######################################################################
#
# Main script execution
#
Test-AdminRights
Test-PowerShellVersion
Enable-LocalPSRemoting
#
foreach ($Computer in $ComputerNames) {
Write-Output "Processing computer: $Computer"
# Uninstall application if ApplicationName is provided
if ($PSBoundParameters.ContainsKey('ApplicationName')) {
Uninstall-Application -AppName $ApplicationName -Computer $Computer
}
# Remove service if ServiceName is provided
if ($PSBoundParameters.ContainsKey('ServiceName')) {
Remove-Service -SvcName $ServiceName -Computer $Computer
}
}
#
#######################################################################