PowerShell で、実行中のプロセスのコマンドライン引数を取得するコード例を書きました。
タスクマネージャーの『プロセス』タブや『詳細』タブにある、『コマンド ライン』の列に表示されているような『コマンドライン引数』を取得します。
『実行中のプロセスの一覧』や『コマンドライン引数の一覧』は、PowerShell で取得することができました。
注意です。
PowerShell の『新しいバージョン (PowerShell 7.1)』と、『古いバージョン (PowerShell 5.1)』では、使用できるコマンドが異なっていました。
PowerShell マニュアル
コード例で使用した機能です。
(PowerShell) Example 1: Get the CIM instances of a specified class (Win32_Process
)
(PowerShell) Get-WmiObject
※ PowerShell 5.1 用
(PowerShell) Example 1: Get processes on the local computer (Win32_Process
) ※ PowerShell 5.1 用
(PowerShell) Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}
PowerShell 7.1
PowerShell 7.1 を使用した場合のコード例と実行結果です。
コード例 (Get-CimInstance)
Get-CimInstance
を使用したコード例です。
コード例は PowerShell 7.1 で実行しました。
実行中のプロセスの一覧を、コマンドラインの引数付きで取得します。
実行中のすべてのプロセスから、指定した文字列を含んだ『コマンドライン引数』を取得しました。
Get-CimInstance -Class Win32_Process | Where-Object {$_.CommandLine -like "*ms-mmsys*"} | Select-Object Name, CommandLine
実行結果 (Get-CimInstance)
実行中のプロセスから、プロセスの名前とコマンドライン引数を取得した結果です。
PowerShell 7.1 で実行しました。
Name CommandLine
---- -----------
rundll32.exe "C:\WINDOWS\System32\rundll32.exe" C:\WINDOWS\System32\shell32.dll,Control_RunDLL C:\WINDOWS\System32\mmsys.cpl ms-mmsys:,{*.*.*.********}.{********-****-****-****-************},general
PowerShell 5.1
PowerShell 5.1 を使用した場合のコード例と実行結果です。
コード例 (Get-WmiObject)
Get-WmiObject
を使用したコード例です。
実行中のプロセスの一覧を、コマンドラインの引数付きで取得します。
実行中のすべてのプロセスから、指定した文字列を含んだ『コマンドライン引数』を取得しました。
コード例は PowerShell 5.1 で実行しました。
Get-WmiObject -Class Win32_Process | Where-Object {$_.CommandLine -like "*ms-mmsys*"} | Select-Object Name, CommandLine
※ Get-WmiObject
は、PowerShell 5.1 用です。
PowerShell 7.1 だと、以下のメッセージが表示されて、使用できませんでした。
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
実行結果 (Get-WmiObject)
実行中のプロセスから、プロセスの名前とコマンドライン引数を取得した結果です。
PowerShell 5.1 で実行しました。
Name CommandLine
---- -----------
rundll32.exe "C:\WINDOWS\System32\rundll32.exe" C:\WINDOWS\System32\shell32.dll,Control_RunDLL C:\WINDOWS\System32\mmsys.cpl ms-mmsys:,{*.*.*.********}.{********-****-****-****-************},general
(補足)このプロセスは、Windows 10 で『マイクのプロパティ』や『スピーカーのプロパティ』を表示したときに現れたプロセスでした。
『追加のデバイスのプロパティ』からプロパティ画面を出したときに、このプロセスが出現しました。
このプロセスの引数を使用したら、マイクやスピーカーのプロパティを直接開くショートカットを作ることができました。
⇒ 『マイク』や『スピーカー』のプロパティを一発で開くショートカットの作り方【Windows 10】
以上です。
Python の psutil を使用した方法も書きました。