4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】Getコマンドの中から特定の値を抽出する方法

Posted at

はじめに

PowerShellでスクリプトを作成していると、「Getコマンドの中から特定の値を抽出したい」と言う時はありませんか?
今回は、こちらの方法をアウトプットします。

抽出するデータとは?

以下、コマンド実行結果のDomainという値を抽出したいと思います。

Get-NetFirewallProfileはファイアウォール設定を確認するコマンドになります。

コマンド実行結果
 PS C:\Users\Administrator> Get-NetFirewallProfile -Name Domain

Name                            : Domain ← これを抽出
Enabled                         : False
DefaultInboundAction            : NotConfigured
DefaultOutboundAction           : NotConfigured
AllowInboundRules               : NotConfigured
AllowLocalFirewallRules         : NotConfigured
AllowLocalIPsecRules            : NotConfigured
AllowUserApps                   : NotConfigured
AllowUserPorts                  : NotConfigured
AllowUnicastResponseToMulticast : NotConfigured
NotifyOnListen                  : False
EnableStealthModeForIPsec       : NotConfigured
LogFileName                     : %systemroot%\system32\LogFiles\Firewall\pfirewall.log
LogMaxSizeKilobytes             : 4096
LogAllowed                      : False
LogBlocked                      : False
LogIgnored                      : NotConfigured
DisabledInterfaceAliases        : {NotConfigured}

データ抽出

こちらのコマンドを実行します。

実行コマンド
  (確認コマンド).項目名

コマンド実行例①

Get-NetFirewallProfileNameの値を抽出したい場合

コマンド実行例
PS C:\Users\Administrator> (Get-NetFirewallProfile -Name Domain).Name
Domain

コマンド実行例②

Get-Serviceからサービス名のみ抽出

コマンド実行例
PS C:\Users\Administrator> (Get-Service -Name ALG).Name
ALG 

※抽出しない場合の表示

 PS C:\Users\Administrator> Get-Service -Name ALG

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  ALG                Application Layer Gateway Service      

最後に

この方法を探す前は、以下記事の方法を使っていました。
【PowerShell】Linuxのgrepのように行を検索する方法(Windows)

しかし、今回紹介した内容の方が短文で使いやすいことがわかりました。
是非、ご活用ください!

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?