- Using PowerShell with $PSStyle - Mon, Jan 24 2022
- Clean up user profiles with PowerShell - Mon, Jun 9 2014
- Track user logons with a PowerShell script - Fri, Jun 6 2014
If you are new to Windows PowerShell, you may be afraid it is too difficult to learn. Or that you have write complicated scripts in order to use it. That couldn’t be further from the truth. PowerShell is designed as an interactive management engine that just happens to allow scripting. The truth is that you can use PowerShell from a text based console and manage your Windows environment with a few simple commands.
To prove this we’re going to look at managing services with PowerShell from a command prompt. The emphasis is on using PowerShell interactively, not trying to script. I’m going to assume you already have PowerShell running on Windows 7 or later and you have admin rights. If you want to follow along, please try to do so in a non-production or virtualized environment. I’ll cover service management using PowerShell v2 and v3.
Get service status ^
Let’s begin by simply getting the status of all the services on the local computer. To accomplish this we’ll use a command called Get-Service. A PowerShell command like this is referred to as a cmdlet.
PS C:\> get-service
PowerShell is generally not case-sensitive. You can see the results in the screenshot below.
Manage Services with PowerShell - get-service
The output looks text but each line is actually a service object. This object-orientation in PowerShell is what trips up many beginners. All we see here is the default display. But each service object has other properties as well. You can discover them by piping the objects to another command, Get-Member.
PS C:\> get-service | get-member
You can see the results in the next screenshot.
Manage Services with PowerShell - get-member
The Typename at the top of the screen tells us what type of object this is, a System.ServiceProcess.ServiceController. When you see a name like this that starts with System, you will know that it is a .NET object. This isn’t too important right now but I wanted you to understand how to discover the object type. I’ve also circled the object properties. These are attributes that describe this type of object. Even though many of these aren’t used in the default display, if you know them you can use them.
For example, let’s say we are only interested in the Windows Update Service. We can ask Get-Service to display just that service and then select a few properties.
PS C:\> get-service wuauserv | select Displayname,Status,Can* DisplayName : Windows Update Status : Stopped CanPauseAndContinue : False CanShutdown : False CanStop : False
How did I know I could type a service name? I looked at help for Get-Service.
PS C:\> help get-service
The screenshot displays the short help. I’m running this in a PowerShell v2 session on Windows 8 so you might see slight variations on older systems.
Manage Services with PowerShell - help get-service
You can get complete help by typing:
PS C:\> get-service wi* Status Name DisplayName ------ ---- ----------- Stopped WiaRpc Still Image Acquisition Events Running WinDefend Windows Defender Service Running WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se... Running Winmgmt Windows Management Instrumentation Running WinRM Windows Remote Management (WS-Manag...
Or, if you are more comfortable with display names, use the –Displayname parameter.
PS C:\> get-service -DisplayName "windows a*" Status Name DisplayName ------ ---- ----------- Stopped AllUserInstallA... Windows All-User Install Agent Running AudioEndpointBu... Windows Audio Endpoint Builder Running Audiosrv Windows Audio
I had to use the parameter name so that PowerShell would treat the values as a display name and not an actual service name. A command like this would fail:
PS C:\> get-service "windows a*"
Because the –Name parameter is positional, meaning you don’t have to type it.
Service status of remote computers ^
So far we’ve been looking at service information on the local computer. Naturally we want to manage services on remote computers. If you look at help for Get-Service you’ll see a –Computername parameter. Connecting to remote computers like this does not use PowerShell’s remoting feature nor does PowerShell remoting have to be enabled anywhere. If you can manage services using command line tools like SC.EXE or the Service Manager management console, you can use PowerShell. Let’s try it out:
PS C:\> get-service spooler -ComputerName novo8 Status Name DisplayName ------ ---- ----------- Running spooler Print Spooler
Any command I’ve shown you so far you can use to query a remote computer. Even multiple computers, assuming your credential has sufficient administrative privileges on the remote computer. If you have PowerShell v3, it is very easy to check a single service on multiple computers.
PS C:\> get-service wuauserv -ComputerName chi-dc01,chi-dc02,chi-dc03 Status Name DisplayName ------ ---- ----------- Running wuauserv Windows Update Stopped wuauserv Windows Update Running wuauserv Windows Update
Although you need to format the output slightly to know which service goes with which computer.
PS C:\> get-service wuauserv -ComputerName chi-dc01,chi-dc02,chi-dc03 | format-table Name,Status,Machinename -autosize Name Status MachineName ---- ------ ----------- wuauserv Running chi-dc03 wuauserv Stopped chi-dc02 wuauserv Running chi-dc01
That’s a one line command that queried the service on 3 different computers and displayed the results in a nicely formatted table and you didn’t write a single script!
To do the same thing in PowerShell v2 takes a slightly more awkward approach, but still no scripting.
PS C:\> 'chi-dc01','chi-dc02','chi-dc03'| foreach {get-service wuauserv -computername $_} | Format-Table Name,Status,Machinename -AutoSize Name Status MachineName ---- ------ ----------- wuauserv Running chi-dc01 wuauserv Stopped chi-dc02 wuauserv Running chi-dc03
Next time we’ll look at another way to accomplish this and other filtering techniques as well as explore more of this service object.
Thanks Mr. Hicks! Exactly the info I needed!
We have used the below Powershell script that sends html status of services
http://msexchange.me/2013/07/07/services-monitor-report/
Vikas, a few comments on your script since you suggested it as a link. I think you are working way too hard. There is no need to manually create the html file. That is what ConvertTo-HTML is for. There is a also a cmdlet, Send-Mailmessage, for email the report. The rule should be to always look for cmdlets to get the job done.
There are many ways to create reports with embedded style sheets which is what you are trying to do. If you check my blog and search for ‘html’ you’ll find a number of examples.
http://jdhitsolutions.com/blog/?s=html
How do we pass credentials while connecting to remote machine.
Jai,look at help for Invoke-Command, Enter-PSSession or New-PSSession You can create the credential object ahead of time with Get-Credential or use the -Credential parameter and specify the user name in the form domain\username or computername\username.
Thanks much for the info.
Hey Jeff, Thanks for the post.
I face an issue when i runt the below command on ps-
get-service wuauserv -computername com-a,com-b | format-table machinename, name, status -autosize
MachineName Name Status
———– —- ——
com-a wuauserv Running
I dont get the results for the second and any other machine which follows the first hostname. Can you suggest me a solution?
The first thing to test is can you run the command for just com-b? What version of PowerShell?
Good Article, I got what I need.
Hi
How Can I get a service with PID for example If I want to see winmgmt with PID
Please provide that cmdlet
Hi Partha, im not sure of the cmdlet, but there is a cmd for it.
Tasklist /svc
Would list all services displaying the pid that they are running on..
You could even try this
Get-wmiobject -class win32_service | {$_.Name -eq <Name of service> | select-Object -property *
PowerShell is really powerful. You can use it to query services from all remote computers in your domain, filter and sort list of services.
For example, this is how you can get list of all services in your AD domain:
Get-ADComputer -Filter {OperatingSystem -Like “Windows 10*”} | ForEach-Object {Get-WmiObject -Class Win32_Service -Computer $_.Name}
More useful examples on this (sort, filter, get services for remote computers listed in a file, etc.):
https://www.action1.com/kb/list_of_services_on_remote_computer.html
Hi Jeff,
I want to create a bat file which will trigger a .ps1 power shell script to get service status of a remote server.
can you please provide me the steps…
Thanks….
You can use the following command in a batch file. The only need for this style of calling Powershell would be needed when using Powershell commands in conjunction with other batch commands…
Just update the NAME_OF_FILE, the script will get the local path using the %~dp0 in-front of the filename.ps1.
Hi Sri – I would really love to know the reason why you want to use a BAT file to run a PS script. I keep seeing this a lot, I feel the need to understand the rational behind it; personally it feels very wrong or at the least unnecessary.