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

More with Process and Service uptime

Posted on February 20, 2007August 5, 2009

Like most things scripting, there's usually more than one way to do things. I thought I had a nice solution for getting service uptime via WMI. But alas, there is an even easier way. PowerShell has a ConvertToDateTime method which will convert a WMI time to a standard date time format.

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!

$p=ps winlogon
$p=get-wmiobject -query "Select * from win32_process where name='winlogon.exe'"

Looking $p.CreationDate will yield something like this:

20070217093525.074160-300

The ConvertToDatetime method can be seen here:

PS S:\PoSH > $p.ConverttoDatetime

Script : [System.Management.ManagementDateTimeConverter]::ToDateTime($args[0])
OverloadDefinitions : {System.Object ConvertToDateTime();}
MemberType : ScriptMethod
TypeNameOfValue : System.Object
Value : System.Object ConvertToDateTime();
Name : ConvertToDateTime
IsInstance : False

So to use it, all I have to do is specify the WMI Time as the argument:

PS S:\PoSH > $p.ConverttoDatetime($p.CreationDate)

Saturday, February 17, 2007 9:35:25 AM

Isn't that much easier than my convoluted string function? I can use this in its place and modify my script.

Here's the final, updated code that will work using WMI.

$computer="."
Write-Host -fore Green -back Black $computer.ToUpper()
$s=Get-WmiObject -query "Select name,processId,state from Win32_service where state='running'" -computer $computer
foreach ($item in $s) {
$query="Select handle,creationdate from win32_process where handle='"+$item.ProcessID+"'"
$p=Get-WmiObject -query $query -computer $computer
$start=$p.ConvertToDateTime($p.CreationDate)
$u=(get-date).Subtract($start)
Write-Host $item.Name `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}

As written, it will connect to the local computer, but you can specify a remote computer name.

Technorati tags: PowerShell, WMI, Scripting, Services, Processes


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

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