Warm tip: This article is reproduced from serverfault.com, please click

How to get the process start time in PowerShell

发布于 2015-12-07 19:57:00

How do I get the process start time in PowerShell?

I tried this in the PowerShell prompt:

(Get-Process MyProcessName).StartTime.ToString('yyyyMMdd')

And this is the error I got:

(Get-Process MyProcessName).StartTime.ToString('yyyyMMdd')

You cannot call a method on a null-valued expression.
At line:1 char:1
+ (Get-Process MyProcessName).StartTime.ToString('yyyyMMdd_h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

But when I do Get-Process MyProcess, I see my process:

> Get-Process MyProcess

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
   2906     132    81136      79132   889           20656 myprocess

And when I do 'Get-Date -format 'yyyyMMdd', it returns '20151207', so I think the date format is correct.

Questioner
n179911
Viewed
0
sodawillow 2015-12-08 04:02:26

Do it like this:

(Get-Date (Get-Process explorer).StartTime).ToString('yyyyMMdd')