Sometime after 7.0, a new ETS script property named CommandLine was introduced for System.Diagnostics.Process instances, so that you could query a given process' command line.; e.g.:
# Currently Windows and Linux only: get the PowerShell session's own command line.
(Get-Process -Id $PID).CommandLine
As an aside: The property doesn't yet support macOS - see #13943.
On Linux, this is in effect implemented via the following command, using Linux' special files:
Get-Content /proc/$PID/cmdline
The problem is that the content of these files contains the verbatim arguments as originally used during process creation separated with NUL characters, which in the terminal - due to NUL getting ignored - presents like directly joined arguments, which is unhelpful to the human observer.
The question is how much effort we want to expend to present a functional command line that is the equivalent of the original:
-
A simple solution would be to simply space-concatenate the verbatim arguments -
(Get-Content /proc/$PID/cmdline) -replace "`0", ' ' - for a friendlier presentation:
-
Trying to recreate a functional command line would require substantially more effort - namely for recreating required quoting and escaping - and has an inherent conceptual problem:
Steps to reproduce
On Linux, from PowerShell"
pwsh -noprofile -command 'Get-Content /proc/$PID/cmdline'
Expected behavior
A string with the following verbatim content:
/usr/bin/pwsh -noprofile -command Get-Content /proc/$PID/cmdline
This assumes simple space concatenation without any attempt to recreated required quoting and escaping; see discussion above.
Actual behavior
Since the verbatim arguments are NUL-concatenated, they appear as directly concatenated; note the lack of spaces between pwsh, -noprofile, -command, and the latter's argument:
/usr/bin/pwsh-noprofile-commandGet-Content /proc/$PID/cmdline
Environment data
PowerShell Core 7.1.0-rc.2
Sometime after 7.0, a new ETS script property named
CommandLinewas introduced forSystem.Diagnostics.Processinstances, so that you could query a given process' command line.; e.g.:As an aside: The property doesn't yet support macOS - see #13943.
On Linux, this is in effect implemented via the following command, using Linux' special files:
The problem is that the content of these files contains the verbatim arguments as originally used during process creation separated with
NULcharacters, which in the terminal - due toNULgetting ignored - presents like directly joined arguments, which is unhelpful to the human observer.The question is how much effort we want to expend to present a functional command line that is the equivalent of the original:
A simple solution would be to simply space-concatenate the verbatim arguments -
(Get-Content /proc/$PID/cmdline) -replace "`0", ' '- for a friendlier presentation:psutility does with-o command=, which is the only similarly simple solution for macOS - see Make the .CommandLine ETS script property on Process objects work on macOS too #13943.Trying to recreate a functional command line would require substantially more effort - namely for recreating required quoting and escaping - and has an inherent conceptual problem:
Steps to reproduce
On Linux, from PowerShell"
Expected behavior
A string with the following verbatim content:
This assumes simple space concatenation without any attempt to recreated required quoting and escaping; see discussion above.
Actual behavior
Since the verbatim arguments are
NUL-concatenated, they appear as directly concatenated; note the lack of spaces betweenpwsh,-noprofile,-command, and the latter's argument:Environment data