PowerShellのコマンドレッドを使用して、Windows OS のサービスの一覧を取得したり、開始、停止などの操作ができます。
この記事では、Powershellを使用したサービス情報の取得と出力の方法を説明します。
サービス関連のコマンドレッド
サービス関連のコマンドレッドを確認するには、以下のコマンドを実行します。
PS> Get-Command *-Service CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Get-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet New-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Restart-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Resume-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Set-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Start-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Stop-Service 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Suspend-Service 3.1.0.0 Microsoft.PowerShell.Management
Get-Help <Cmdlet-Name>を使うと、各コマンドレットに関する情報を確認できます。
以下は各コマンドの説明です。
コマンドレッド名 | 説明 |
Get-Service | ローカルおよびリモートコンピューター上のサービスを取得します。 |
New-Service | 新しい Windows サービスを作成します。 |
Restart-Service | サービスを再起動します。 |
Resume-Service | 中断 (一時停止) 中のサービスを再開します。 |
Set-Service | サービスのプロパティを変更します。 |
Start-Service | 停止しているサービスを開始します。 |
Stop-Service | 実行中のサービスを停止します。 |
Suspend-Service | 実行中のサービスを中断 (一時停止) します。 |
サービス情報の取得
サービスに関する情報は、「Get-Service」コマンドレットで取得できます。
Get-Serviceの形式
Get-Service [-Name] <サービス名> Get-Service -DisplayName <サービスの表示名>
Get-Serviceの実行例
状態、サービス名、表示名が表示されます。
PS> Get-Service Status Name DisplayName ------ ---- ----------- Running AdobeARMservice Adobe Acrobat Update Service (省略) Stopped ALG Application Layer Gateway Service (省略)
Get-Serviceは、システムにインストールされているサービスの数を表示することもできます。
サービスの数を表示する例
PS> (Get-Service).Length 299 PS> (Get-Service).Count 299
サービス情報の出力
Powershellでは、サービスの情報をファイルに保存することができます。
リダイレクト記号(>)を指定する方法の例
PS> Get-Service >c:\temp\ServiceList.txt
実行すると、指定したフォルダにファイルが作成されます。

Out-Fileコマンドレッドを使用してファイルへ出力する例
PS> Get-Service | ` Where-Object{$_.Status -eq "running"} | ` Out-File -FilePath c:\temp\ServiceRunningList.txt
この例では、Get-Serviceコマンドレッドの結果をWhere-Objectコマンドレッドで状態が”running”のデータを抽出し、指定したファイルに結果を出力します。

Export-CSVコマンドレッドを使用してファイルへ出力する例
PS> Get-Service |
Where-Object{$_.Status -eq "running"} |
Select-Object Status,Name,DisplayName| ` Export-Csv -Path c:\temp\ServiceRunningList.txt -Encoding utf8
実行すると、Select-Objectで指定した項目のCSVファイルが作成されます。

まとめ
Powershellを使用したサービス情報の取得と出力の方法について説明しました。