Divya Bais
Divya Bais

Reputation: 1

How to display the count of running processes through powershell script

Just trying to find out how do we get the count of running services through powershell script. I know Get-Service will give us list of all the processes on the system and (Get-Service). Count will give the count. But question is how do we find the count of only Running Services in the system? I simplified the filter criteria to get the list of only running services which is Get-Service | Where-Object {$_.Status -eq "Running"} BUT NO LUCK WITH THE COUNT OF THOSE SERVICES.

Upvotes: 0

Views: 4187

Answers (1)

gvee
gvee

Reputation: 17161

TL;DR

(Get-Service | Where-Object Status -eq "Running").Count

Have you heard of the Get-Member CmdLet?

If not; get reading up on it here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member?view=powershell-6

Gets the properties and methods of objects.

I find this one of the most useful commands in situations similar to this. Get used to using it as it will come in very handy!

How does that help you? Well...

Get-Service | Get-Member

Returns:

   TypeName: System.ServiceProcess.ServiceController

Name                      MemberType    Definition
----                      ----------    ----------
Name                      AliasProperty Name = ServiceName
RequiredServices          AliasProperty RequiredServices = ServicesDependedOn
Disposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)
Close                     Method        void Close()
Continue                  Method        void Continue()
CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose                   Method        void Dispose(), void IDisposable.Dispose()
Equals                    Method        bool Equals(System.Object obj)
ExecuteCommand            Method        void ExecuteCommand(int command)
GetHashCode               Method        int GetHashCode()
GetLifetimeService        Method        System.Object GetLifetimeService()
GetType                   Method        type GetType()
InitializeLifetimeService Method        System.Object InitializeLifetimeService()
Pause                     Method        void Pause()
Refresh                   Method        void Refresh()
Start                     Method        void Start(), void Start(string[] args)
Stop                      Method        void Stop()
WaitForStatus             Method        void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStat...
CanPauseAndContinue       Property      bool CanPauseAndContinue {get;}
CanShutdown               Property      bool CanShutdown {get;}
CanStop                   Property      bool CanStop {get;}
Container                 Property      System.ComponentModel.IContainer Container {get;}
DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}
DisplayName               Property      string DisplayName {get;set;}
MachineName               Property      string MachineName {get;set;}
ServiceHandle             Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}
ServiceName               Property      string ServiceName {get;set;}
ServicesDependedOn        Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}
ServiceType               Property      System.ServiceProcess.ServiceType ServiceType {get;}
Site                      Property      System.ComponentModel.ISite Site {get;set;}
StartType                 Property      System.ServiceProcess.ServiceStartMode StartType {get;}
Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}
ToString                  ScriptMethod  System.Object ToString();

Isn't that cool?!

Take a quick look at the properties available (Get-Service | Get-Member -MemberType Property) - spot anything useful?

(Get-Service | Where-Object Status -eq "Running").Count

Upvotes: 2

Related Questions