Use WMI and PowerShell to Work with Services
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to work with services.
Microsoft Scripting Guy, Ed Wilson, is here. It is that time of the year. I just spent the last couple of hours uninstalling useless software from my laptop. This includes stuff that I intentionally installed and junk that becomes installed with other programs. I also spent a decent amount of time reviewing start-up programs and services. This is pretty much a stop gap measure – it is only a matter of time before I FDISK my machine and perform a complete reinstall.
It still seems necessary to reinstall every year, which is a lot better than it used to be. I can remember a time when I reinstalled twice a year. The Scripting Wife has been talking about reinstalling her laptop for a while. I was kind of hoping to avoid doing all that until Windows 10 ships, but now I am not so sure.
The problem is that although reinstallation is pretty easy these days, finding all the keys, original disks, and so on that seems to take forever. In my case, it seems that I continually find stuff that I am missing for weeks.
I am in the middle of a project now, so I want to avoid that huge time hit for as long as I can. But I did feel the need to do some maintenance and optimization. Luckily, I have Windows PowerShell to aid in the task.
Using native Windows PowerShell commands for services
Windows PowerShell has always had the Get-Service cmdlet. It is great for what it does—and that is to show basic service information. By default, it has an output such as the following:
As shown here, I can easily sort the output by piping it to the Sort-Object cmdlet:
Get-Service | sort status –Descending
To see the running processes first, I need to use the –Descending switch because R (for running) comes before S (for stopped).
I can also get an overview of how many started and stopped services I have by using the Group-Object cmdlet. This technique is shown here:
PS C:\> Get-Service | sort status -Descending | group status -NoElement
This is all based on the default output. What information is available via the Get-Service cmdlet? The best way to find out is to select a single service and send the output to the Format-List cmdlet. This is shown here:
If I want more information about the services, I need to use WMI. Here is the default output when I use the Get-CimInstance Win32_Service command:
To get a better look at what is available, I use the –Filter parameter to return only information about the Bits service:
Get-CimInstance Win32_Service -Filter "name = 'bits'" | fl *
The output is shown in the following image:
For the purposes of optimizing my laptop, I like the following properties:
This information tells me what the service does, what account it uses to start up, how the service starts, and even the executable it uses. This information helps me make the decisions I need. Lately, I have found that when I search for information about services on the Internet, I get pages of spam sites that are useless (at best).
This is why I am glad that I can get this kind of detailed information via Windows PowerShell. When I need additional information, I search TechNet or MSDN directly, and I can normally find good stuff there.
I use the following command to obtain the output from WMI:
$p = 'name', 'startname', 'startmode', 'pathname', 'description'
Get-CimInstance Win32_Service -Filter "state = 'running'" -Property $P | FL $P
The command and its output are shown here:
That is all there is to using WMI and Windows PowerShell to work with services. WMI Week will continue tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy