Menu

[ea00bd]: / util-winUninstall / Uninstall-AppAndService.ps1  Maximize  Restore  History

Download this file

154 lines (147 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#
# 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
}
}
#
#######################################################################
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.