PowerShell Pipe & Select
Using PowerShell to Pipe Get-Service to Stop-Service
Let's break down the command Get-Service -Name RemoteRegistry | Stop-Service & explain it:
​
-
Get-Service -Name RemoteRegistry: This command retrieves the status and information about the service named RemoteRegistry. The Get-Service cmdlet gets the status of services on a local or remote computer. The -Name parameter specifies the service name.
​
-
| (Pipeline Operator): The pipeline operator (|) passes the output of the command on its left (in this case, Get-Service -Name RemoteRegistry) as input to the command on its right (Stop-Service).
​
-
Stop-Service: This cmdlet stops a running service. When Stop-Service receives the input from the pipeline, it uses the service object provided by Get-Service.
​
Detailed Explanation
Here's how the command works step by step:
​
-
Retrieve the Service: The Get-Service -Name RemoteRegistry command gets the RemoteRegistry service. This cmdlet returns a service object containing information such as the service's status, display name, and more.
-
Pipe the Service Object: The pipeline operator (|) passes the service object from Get-Service to Stop-Service.
-
Stop the Service: The Stop-Service cmdlet takes the service object received from the pipeline and stops the RemoteRegistry service.
Get-Service | Select Name, Status | Format-Table
As you can see in the example below the data pulled from Get-Service is passed using the | (Pipe) to the select statement and then passed again to the Where-Object and the final output is only the data of running services.