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()
}
}