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#?
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#?
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.
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
If you put the following code in your powershell $PROFILE
file you can permanently extend the Process
object class and use the CommandLine
property:
$TypeData = @{
TypeName = [System.Diagnostics.Process].ToString()
MemberType = [System.Management.Automation.PSMemberTypes]::ScriptProperty
MemberName = 'CommandLine'
Value = {
if (('Win32NT' -eq [System.Environment]::OSVersion.Platform)) { # it's windows
(Get-CimInstance Win32_Process -Filter "ProcessId = $($this.Id)").CommandLine
} elseif (('Unix' -eq [System.Environment]::OSVersion.Platform)) { # it's linux/unix
Get-Content -LiteralPath "/proc/$($this.Id)/cmdline"
} elseif (('MacOSX' -eq [System.Environment]::OSVersion.Platform)) { # it's macos
# ???
}
}
}
Update-TypeData @TypeData -ErrorAction Ignore
NB:
Update-TypeData
is called with-ErrorAction Ignore
because in pwsh (at least on version 7.3.4),CommandLine
already exists;-EA Ignore
suppresses the error. As an alternative, you could check for the property existence, and executeUpdate-TypeData
only in the case of missing property.
The scriptblock used as value is taken from what pwsh 7.3.4 actually uses internally, adapted also for Windows Powershell (where $IsWindows
, etc do not exist).
You can get the code in the scriptblock by running the following in pwsh 7.3.4: (([System.Diagnostics.Process]@{}) | gm | ? { $_.Name -ieq 'commandline' }) | select -expand Definition
.
Then you can reliably query the command line (iif you have the correct rights, for the queried process(es), see [1], [2]):
get-process notepad.exe | select-object ProcessName, CommandLine
I'm using powershell 7.1 and this seems to be built in to the process object now as a scripted property:
> (Get-Process notepad)[0].CommandLine
"C:\WINDOWS\system32\notepad.exe"
Interestingly, you can view its implementation and see that it partially uses the answer from PsychoData:
($process | Get-Member -Name CommandLine).Definition
System.Object CommandLine {get=
if ($IsWindows) {
(Get-CimInstance Win32_Process -Filter "ProcessId = $($this.Id)").CommandLine
} elseif ($IsLinux) {
Get-Content -LiteralPath "/proc/$($this.Id)/cmdline"
}
;}
Running Get-Member on a process shows that it is an instance of System.Diagnostics.Process, but that it has several properties that are scripted.
The other properties are FileVersion, Path, Product, and ProductVersion.