PowerShell Filter and Get-Service Cmdlet
Introduction
This article is for helping who are new to PowerShell. Step by Step we'll try to explain so that you can easily understand. This part is about only Filters and Get-Service Cmdlet. In PowerShell there are GET, SET main Cmdlets. GET Cmdlets can get information from computers. But SET Cmdlets can set some settings or configurations on a computer. All Cmdlets and Scripts always use filters. Let's begin with FILTERs.
Details
Get Cmdlets so many times uses Filters, because when you are getting some information from a computer it brings so many information and we need to shorten them for easy reading or as per your requirement.
For example:
This Cmdlet will get all service from a Local Computer.
Get-Service
If we need to get the service list from another computer then we will need to type:
**Get-Service -Computername ***hp-cnd4251cz *
So many time we are bored to type long commands and at that time we can use (Alias) short versions of Cmdlet. The short version of **Get-Service **Cmdlet is gsv.
Instead of typing Get-Service, we can use gsv Cmdlet (Alias) as well.
Lets to go to filters as mentioned above.
When we get the service list from a computer, we get the Status, Name, DisplayName of the services. This headers are important for selection of exact column.
For example: we need to list only services where Status is Running. Then we will type:
**Get-Service | Where {$_.Status -eq "Running"}
**Note: Where is a alias for Where-Object Cmdlet.
As seen on the above code, for filtering I've used **Where **Cmdlet.
**$_.Status **--> means 'look to Status column' & -eq "Running"--> means equal to Running.
As you see there is a **-eq ** which is a comparison operator. There are several types of comparison operators which helps us to collect exact info we want.
Here are some Comparison Operators:
-eq | Means: equal | Example: -eq "Running" Not support * symbol, only need to type full name |
-le | Means: less or equal |
Example: -le 300 (Less or equal 200) or (<=300) |
-lt | Means: less than | Example: -lt 200 (Less than 200) or (<200) |
-like | Means: like | Example: -like "Running"or -like "Run*" or -like "*unnin* |
-notlike | Means: not like | Example: -notlike "stopped" or -notlike "St*" or -notlike "*oppe*" |
-ge | Means: Greater or equal | Example: -ge 200 (Greater or equal to 200) or (>=200) |
-gt | Means: Greater than | Example: -gt 200 (Greater than 200) or (>200) |
Example commands:
Command: list services where is status equal to running only
Get-Service | where {$_.Status -eq "Running"}
Command: list services which name begins with S letter
Get-Service | where {$_.Name -like "S*"}
Command: list services which display name include S letter Get-Service | where {$_.DisplayName -like "*S*"}
Sometimes we will need to filter on one or more conditions:
For example we will need to filter service list which name begins with S letter and the status is equal to Running. Then we will use **-and operator.
If we use -or **or **-and **operator, then we need to use ( ) in the where filter.
like this: Get-Service | where {() -and ()}
And here is full example: **Get-Service | where {($_.Name -like "S*")-and ($_.Status -eq "Running")}
**
****
As we know some services depends on another services and some other services depend on these services.
Get-Service Cmdlet has two switch: DependentServices & RequiredServices. You can also type DS which is short for **DependentServices **and SDO which is short for RequiredServices.
DependentServices will get the services that depend upon the specified service.
RequiredServices will gets only the services that this service requires.
Here are some examples:
gsv -name http -DS *the list of services we get depends on Http services. And it means that if we stop Http service, other services which are depends on this service will not work properly.
**
gsv -name RpcCs -SDO
***the list of service we get are services which is required by this service.
*
Help for PowerShell Cmdlet
To display help for a specific PowerShell Cmdlet:
- Open Windows PowerShell or Windows PowerShell Integrated Scripting Environment (ISE).
- Type Get-Help <cmdlet> -example Or
- Type Get-Help <cmdlet> -full
Example
>Get-help Get-Service -full
>Get-help Get-Service -example