Software Uninstall Automation [Part 1/3]


 

  • Query software with Get-WmiObject.
  • Query software with type accelerator [wmi].
  • Compare efficiency.

Uninstalling software can be a simple task right? but when you have a bunch of machines that must have some software uninstalled, usually due to urgent causes like license expiration, bugs in the infrastructure and so on, this simple task could turns into a big headache, so let’s automate it with Powershell!


Query software with Get-WmiObject

We can use Get-WmiObject cmdlet to query installed softwares like this:

gwmi win32_product | ? {$_.name -like "*software*"}

Use -ComputerName parameter if you want to run this command in a remote machine:

gwmi win32_product -cn hostname| ? {$_.name -like "*software*"}
Aliases:
 gwmi = Get-WmiObject
 ? = Where-Object
-CN = -ComputerName
Positional parameter:
-Class = Win32_Product

Now let’s query a real software, in this example we’ll choose Cisco Jabber Video for Telepresence, but before doing that, let’s see how long this simple query would take using the Measure-Command cmdlet.

Seconds

We can see that it takes long 35 seconds only to query this single software in our local machine, to do this stuff for our bunch of remote machines we must speed things up!


Query software with type accelerator ‘[wmi]’

The type accelerator [wmi] is a more efficient manner to use use WMI classes. but this method returns a specifc instance of a WMI class, so we’ll need specify 3 keys of our object, these are “Name, Version and IdentifyingNumber”.

$Keys = gwmi win32_product | ? {$_.name -like "*jabber*"} | select name,version,id*

select

We stored the keys in the $Keys variable:
$Keys = [command]
To select only these keys we used: 
| select name,version,id*
Wildcard (*):
id* = "IDentifyingNumber"

Note that now we can call each key by using $keys.[property]:

keys

With that said, let’s build our classkey:


COMPARE EFFICIENCY:

Now let’s measure the time of our new query using the [wmi] type accelerator:

Measure-Command {([wmi]"\\localhost\root\cimv2:Win32_Product.$classKey")} | select seconds 

type_accelerator

Look that! the image above is self explanotory isn’t it? we could go from 35 seconds to an instant WMI query!

If this post helped you, or if you have any suggestions please feel free to leave a comment! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.