27
Jan 15

The background for this post was I needed to uninstall our software application(s) from multiple servers and each server could have multiple components installed.

#==================================================================
#==== Must have a text file with servers names in it to run against
#==== Be careful...this scripts just goes and executes....be careful with keeping server names in the txt file.
#==================================================================

$servers = get-content -Path "C:\ScriptyBits\ServerNames.txt"
ForEach ($server in $servers){
$apps=(gwmi win32_product -CN $server | where-object {$_.Name -match "Awesome Application One" -or $_.Name -match "Awesome Application Two"})
ForEach($app in $apps){
$app.Uninstall()
}
}

The above script worked just fine. However, I often setup development environments and rapidly iterate through code versions in which I need to quickly uninstall/re-install our software.

Rather than having to explicitly name each application we install using the "-match" switch I converted it to a use the "-like" switch. This allowed me to point to a list of servers and assuming I had admin rights over all of them it would allow me to uninstall the applications by using wildcards "*"

#==================================================================
#==== Must have a text file with servers names in it to run against
#==== Be careful...this scripts just goes and executes....be careful with keeping server names in the txt file.
#==================================================================

$servers = get-content -Path "C:\ScriptyBits\ServerNames.txt"

ForEach ($server in $servers){
$apps=(gwmi win32_product -CN $server | where-object {$_.Name -Like "*Awesome*"})
write-host Connecting to $server
ForEach($app in $apps){
write-host Uninstalling $app
$app.Uninstall()
}
}

2 Responses for "{ PowerShell } - Uninstall multiple software applications using wildcards"

  1. Rob D says:

    Using win32_product is bad news, it will initialize each of your MSI installations to go into repair mode, which can cause issues (i.e. a service is disabled that was installed with an app, the repair causes it to be enabled again, etc.)...I would especially not run it against a server.

    It's really too bad, too, the win32_product class could be very powerful, but is crippled by this problem.

    https://sdmsoftware.com/group-policy-blog/wmi/why-win32_product-is-bad-news/

Leave a Reply