Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Command Line info for a process in PowerShell or C#

e.g: if I run notepad.exe c:\autoexec.bat,

How can I get c:\autoexec.bat in Get-Process notepad in PowerShell?

Or how can I get c:\autoexec.bat in Process.GetProcessesByName("notepad"); in C#?

like image 829
victorwoo Avatar asked Jul 10 '13 05:07

victorwoo


People also ask

How do I get information from a PowerShell command?

Description. The Get-Help cmdlet displays information about PowerShell concepts and commands, including cmdlets, functions, Common Information Model (CIM) commands, workflows, providers, aliases, and scripts. To get help for a PowerShell cmdlet, type Get-Help followed by the cmdlet name, such as: Get-Help Get-Process .

How do I view a process in PowerShell?

To get started, open up your PowerShell console and run Get-Process . Notice, that Get-Process returns the running process information, as shown below. The output format is identical for the Windows and Linux operating systems. Using the Get-Process cmdlet on Windows to display local processes.

How do I get the PowerShell command-line?

The command to open Command Prompt from Windows PowerShell is exactly the same as the command to open Command Prompt from Command Prompt. In Windows PowerShell, just type start cmd.exe and press Enter.

How do I find the process ID in PowerShell?

To find the PID of a process, type Get-Process . Indicates that the UserName value of the Process object is returned with results of the command. Specifies one or more process objects. Enter a variable that contains the objects, or type a command or expression that gets the objects.


2 Answers

This answer is excellent, however for futureproofing and to do future you a favor, Unless you're using pretty old powershell (in which case I recommend an update!) Get-WMIObject has been superseded by Get-CimInstance Hey Scripting Guy reference

Try this

$process = "notepad.exe" Get-CimInstance Win32_Process -Filter "name = '$process'" | select CommandLine  
like image 30
PsychoData Avatar answered Sep 29 '22 02:09

PsychoData


In PowerShell you can get the command line of a process via WMI:

$process = "notepad.exe" Get-WmiObject Win32_Process -Filter "name = '$process'" | Select-Object CommandLine 

Note that you need admin privileges to be able to access that information about processes running in the context of another user. As a normal user it's only visible to you for processes running in your own context.

like image 157
Ansgar Wiechers Avatar answered Sep 29 '22 02:09

Ansgar Wiechers