Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell Process Uptime

Posted on February 15, 2007July 2, 2013

Not too long ago, I wrote an MCPMag Tip Sheet column on using the pipeline in PowerShell. I showed how you could get the start time of a specified service:

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

$svcname=Read-Host "Enter a service name" ; get-process | where {$_.id -eq (get-wmiobject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime

We can take this a step further and calculate the Uptime. Let's look at a quick one-line example using a process:

(get-date).Subtract((Get-Process firefox).starttime)

This expression subtracts the start time property of the Firefox process from the current date and time. When executed, you should get output like this:

Days : 1
Hours : 4
Minutes : 20
Seconds : 29
Milliseconds : 506
Ticks : 1020295067859
TotalDays : 1.18089706928125
TotalHours : 28.34152966275
TotalMinutes : 1700.491779765
TotalSeconds : 102029.5067859
TotalMilliseconds : 102029506.7859

If I wanted nicer output from my one-liner I can clean it up like this:

$u=(get-date).Subtract((Get-Process firefox).starttime);Write-Host $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds

This is one single line expression.

Since all services are associated with a process, we can find the uptime of the process and thus the service. Except for some shared services that use svchost, this should be pretty accurate. I've split this back up into separate expressions to make it easier to follow:

$svcname=Read-Host "Enter a service name"
$p=Get-Process | where {$_.id -eq (Get-WmiObject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime
write-host $svcname service
$u=(get-date).Subtract($p.StartTime)
Write-Host $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds

Not too shabby. The process is a little tedious because I have to query each service via WMI to get it's associated process. To wrap this up, let's go whole hog and report on the uptime for each running service. You'll really need to put this in a script.

Function Get-ProcessUptime {
param([string]$svcname)
$p=Get-Process | where {$_.id -eq (Get-WmiObject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime
$u=(get-date).Subtract($p.StartTime)
Write-Host $svcname service `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}

foreach ($svc in (get-service | where {$_.status -eq "running"})) {Get-ProcessUptime $svc.name}

I've put the uptime code in a function that takes a service name as a parameter. I then get a list of running services and pass each one to the function. There's probably a more efficient way to do this, but it doesn't jump out at me right now.

Bonus:

Get uptime for all processes:

foreach ($proc in (Get-Process | where {$_.name -ne "Idle" -AND $_.name -ne "System"})) {
$u=(get-date).Subtract($proc.starttime);
Write-Host $proc.name `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}

Technorati tags: Powershell, Scripting, Pipeline, MCPMag, TipSheet, Uptime


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “PowerShell Process Uptime”

  1. Anonymous says:
    September 26, 2007 at 3:36 pm

    Are you aware of any way to do this remotely? The starttime doesn’t seem to be available from Win32_process through WMI.

  2. Jeffery Hicks says:
    September 26, 2007 at 6:28 pm

    As you are aware Get-Process only works locally. You could get the same result but you’d need to use Get-WMIObject and the Win32_Process class. There is CreationDate property you could use, although you’d need to convert it to a usable date format. I don’t have anything handy in Powershell to show you. But you might take a look at my Mr. Roboto column from August 2007 (http://redmondmag.com/columns/article.asp?EditorialsID=1860)
    I wrote an HTA that reports on processes for any given computer including how long the process has been running. It’s VBScript but it might get you on the right track until I can put something together for PowerShell.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d