Get-Service cmdlet [Continuation] | PowerShell
Tags: dynamicnavbc, powershell, csv, dataexport
Published: 2022-01-06 17:43:46

---
title: Get-Service cmdlet [Continuation] | PowerShell
published: true
date: 2021-12-20 17:07:30 UTC
tags: DynamicNAVBC,Powershell,CSV,DataExport
canonical_url: 
---

 **Version:**  5.1.x

As the title suggests, let’s continue to explore the **[Get-Service](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-service?view=powershell-7.2)** PowerShell cmdlet and the inherent regular tasks that could come with it.

We ended on the previous PowerShell category [**post**](https://dev.to/toolgbrmaker/get-service-cmdlet-powershell-4jfk) with a customized view by using the **[Select PowerShell cmdlet](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.2)** and now we’ll go a deep further by sharing some examples on where we can use the cmdlet.

When we speak about services, the most common tasks that come to our mind, are tasks like start, stop, restart and display results as a report. But, how to use the acquired knowledge, till now, to do it!?

As first step we need to start by getting the list of services where we want to perform one of the above tasks…

```powershell
Get-Service -ComputerName 'PTPOPF247203' -DisplayName '*Pharma*'
```

Now, let’s add one of the mentioned tasks on our pipeline as next instruction. I’ll start with the [Restart-Service](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/restart-service?view=powershell-7.2) cmdlet.

```powershell
#restarting the filtered services
Get-Service -ComputerName 'PTPOPF247203' -DisplayName '*Pharma*' |
Restart-Service -PassThru
```

Your list of services will get restarted in sequence with the above command avoiding manual work like navigating throw instances and, one by one restarting them. But, well, why not strive for a little bit of elegance? Even, if your stopped instance gets started with the [**Restart-Service**](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/restart-service?view=powershell-7.2) cmdlet, I’m not comfortable with this lack of distinction between services status. Let’s increment a check to our script and give a more visual output of the things that are being done backstage.

To accomplish that, I’ll use the **[Where-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.2)** cmdlet, and here comes a suggestion of how can be done.

```powershell
#starting the stopped services or restarting the running ones
Get-Service -ComputerName 'PTPOPF247203' -Name '*Pharma*' |
Where-Object { 
    if ($_.Status -eq 'Running') {
        Write-Host 'Attempting to restart' $_.DisplayName 'with status' $_.Status -BackgroundColor DarkRed
        Restart-Service $_
    }
    elseif ($_.Status -eq 'Stopped') {
        Write-Host 'Attempting to start' $_.DisplayName 'with status' $_.Status -BackgroundColor DarkGreen
        Start-Service $_
    }      
}
```

Result:

![](https://toolgbrmaker.files.wordpress.com/2021/12/image-27.png?w=1000)

Now I’m better with myself… Looks much more intuitive this script, when we read it and we can understand better its goal.

We’re now able to manage the status of the services, therefore, we’re only missing examples of how to report the results from our scripts. On the following two examples I’ll use common formats, that are CSV and HTML.

For CSV extraction I’ll use the cmdlet **[Export-Csv](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.2)** and can be seen on the following script suggestion:

```powershell
#Exporting the filtered services -CSV
Get-Service -ComputerName 'PTPOPF247203' -Name '*Pharma*' |
Select-Object DisplayName,Status |
Export-Csv "C:\Bruno_s Documents\Powershell Scripts\ServicesCSV.csv"
```

And by using the above code you should get something similar to the result on the image (some data manipulation was needed on the excel side to reach the table disposal)…

![](https://toolgbrmaker.files.wordpress.com/2021/12/image-28.png?w=1024)

If we want a result, able to be displayed on a browser… we can approach it by using an HTML file data extraction. Let’s see how we can do it.

```powershell
#Exporting the filtered services -HTML
Get-Service -ComputerName 'PTPOPF247203' |
Where-Object -Property Status -eq -Value "Running" |
Select-Object DisplayName,Status |
ConvertTo-Html |
Out-File -FilePath 'C:\Bruno_s Documents\Powershell Scripts\stats.htm'
```

![](https://toolgbrmaker.files.wordpress.com/2021/12/image-29.png?w=822)

On the script, I needed to combine the **[ConvertTo-Html](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-html?view=powershell-7.2)** cmdlet and also **[Out-File](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7.2)** cmdlet. After file creation, I only needed to open the file on the browser.

I’m ending by here this long post, hoping that wasn’t too massive and that this sharing could help you in some way.

For a better understanding, go ahead and create your own scripts by mixing some of the shared concepts with the goal of getting your own different results.

Stay safe!