12
9

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】コマンド出力結果を変数に入力する方法

Posted at

はじめに

コマンド出力結果を変数に入力する方法をアウトプットします。

コマンド

command
 <変数> = <実行したいコマンド>

実行例

実行コマンド例
$test = Get-Host
Write-Output $test 
実行結果
PS C:\Users\Administrator> C:\Users\Administrator\Desktop\scripts\dns_check\test.ps1


Name             : Windows PowerShell ISE Host
Version          : 5.1.17763.1007
InstanceId       : 02356453-d661-49b1-9bff-29f1aa00ea35
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

PS C:\Users\Administrator>  

よく使用するテクニック

コマンド実行結果の中の1つのオブジェクトのみ入力したい場合

実行コマンド
$test = Get-Host | Select-Object Name
Write-Output $test 
実行結果
PS C:\Users\Administrator> C:\Users\Administrator\Desktop\scripts\dns_check\test.ps1

Name                       
----                       
Windows PowerShell ISE Host

PS C:\Users\Administrator>  

特定の行のみ抜き出す方法

例1

実行コマンド
$test = Get-Host | Out-String -Stream | Select-String "ISE"
Write-Output $test 
実行結果
 PS C:\Users\Administrator> C:\Users\Administrator\Desktop\scripts\dns_check\test.ps1

Name             : Windows PowerShell ISE Host
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions

PS C:\Users\Administrator>  

例2

実行コマンド
$test = Get-Host | Select-Object Name | Out-String -Stream | Select-String "ISE"
Write-Output $test 
実行結果
PS C:\Users\Administrator> C:\Users\Administrator\Desktop\scripts\dns_check\test.ps1

Windows PowerShell ISE Host

PS C:\Users\Administrator>  
12
9
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
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?