Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the process start time in PowerShell

Tags:

powershell

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.

like image 220
n179911 Avatar asked Dec 07 '15 19:12

n179911


People also ask

What is Start process in PowerShell?

The Start-Process cmdlet allows you to run one or multiple processes on your computer from within PowerShell. It's designed to run a process asynchronously or to run an application/script elevated (with administrative privileges).

How does PowerShell calculate time?

Using the Measure-Command cmdlet in PowerShell is an easy way to measure the run-time or execution time of a command. This can also tell you how long a custom function or an entire script takes to execute. Measure-Commandis a cmdlet that is easy to use and provides intuitive, actionable output.


2 Answers

As a one-liner you could use:

Get-Process ProcessName | select starttime
like image 150
gwtharg Avatar answered Oct 15 '22 04:10

gwtharg


Do it like this:

(Get-Date (Get-Process explorer).StartTime).ToString('yyyyMMdd')
like image 36
sodawillow Avatar answered Oct 15 '22 05:10

sodawillow