Home > Enterprise >  How to get "Comand Line" property of a Process in powershell
How to get "Comand Line" property of a Process in powershell

Time:10-08

I wonder how I can get the "the command line" property of a process shown in the Task Manager . I mean, if I run the following

Get-Process -Name "Firefox" | ? {$_.TotalProcessorTime -ne $Null} | Select-Object -Property Name, Id, Path

I get :

Name       Id Path                                         
----       -- ----                                         
firefox   728 C:\Program Files\Mozilla Firefox\firefox.exe   
firefox  2260 C:\Program Files\Mozilla Firefox\firefox.exe  
firefox  2612 C:\Program Files\Mozilla Firefox\firefox.exe    
firefox  3992 C:\Program Files\Mozilla Firefox\firefox.exe   

But I dont need the "Path" property but Command Line instead , I mean for example:

Name   Id    **Command Line**                                                             
----   --  ----                                                                      
firefox 728 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.3.14958**                               
firefox 2260 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.4.9583**                               
firefox 2612 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.5.1392**                                
firefox 3992 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.6.21397**                                

And later on filter ( do a grep) of a part of a string found the command line column ( for e.g:1306.5.1392) I know I can use findstr -i "1306.5.1392" , but not sure if it`s the smartest way

Many thanks in advance for your help!

Kind regards

CodePudding user response:

Query instances of the Win32_Process WMI class to get the command line:

Get-CimInstance -ClassName Win32_Process -Filter "Name = 'firefox.exe'" |Where-Object { 
    (Get-Process -Id $_.ProcessId).TotalProcessorTime -ne $null 
} |Select Name,ProcessId,CommandLine

If you want to include the TotalProcessorTime value in the output, use Select-Object before filtering:

Get-CimInstance -ClassName Win32_Process -Filter "Name = 'firefox.exe'" |Select-Object Name,ProcessId,CommandLine,@{Name='TotalProcessorTime';Expression={(Get-Process -Id $_.ProcessId).TotalProcessorTime}} |Where-Object { $_.TotalProcessorTime -eq $null }

CodePudding user response:

The name is wmi includes the .exe, so firefox.exe. Or

get-ciminstance win32_process | ? name -eq firefox.exe | select commandline

Powershell 7's get-process also has the commandline property. Just return the string with foreach-object.

get-process firefox | foreach commandline
  • Related