{"id":4082,"date":"2013-03-02T00:01:00","date_gmt":"2013-03-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/02\/weekend-scripter-use-powershell-to-display-process-name-and-uptime\/"},"modified":"2013-03-02T00:01:00","modified_gmt":"2013-03-02T00:01:00","slug":"weekend-scripter-use-powershell-to-display-process-name-and-uptime","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-display-process-name-and-uptime\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Display Process Name and Uptime"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a custom table that displays the process name and uptime.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Sometimes it seems as if the Scripting Neighbors are crazy. Yep, it is early on a Saturday morning, and at least one of my neighbors is outside mowing the grass. Dude, it is not even June yet&mdash;what&rsquo;s up with that? Oh well, their lawn mower makes a cheap alarm clock. I guess that is what neighbors are for&mdash;even Scripting Neighbors. So, while I am making a pot of tea, and wondering how long the neighbors have been up, I also begin to wonder how long certain applications on the computer have been up. So I slide over to the Scripting Wife&rsquo;s computer and begin to play.<\/p>\n<h2>Find all processes that return a process start time<\/h2>\n<p>I use the <strong>Get-Process<\/strong> cmdlet to return all processes, and I pipe the results to the <strong>Where-Object<\/strong>. Then I look for the existence of <strong>StartTime<\/strong>. I select only the name and <strong>StartTime<\/strong> because that is what I am interested in seeing. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">gps | ? starttime | select name, starttime<\/p>\n<h2>Create a time span<\/h2>\n<p>Now I need to create a <strong>TimeSpan <\/strong>object. I can do this by using the <strong>New-TimeSpan<\/strong> cmdlet. In the following command, I practice with the Notepad process, while I figure out how to best display the newly created <strong>TimeSpan<\/strong>.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (New-TimeSpan -start (gps notepad).starttime).tostring(&#8220;g&#8221;)<\/p>\n<p style=\"padding-left: 30px\">7:24:02.2344579<\/p>\n<p>I finally decide that rather than creating a custom <strong>TimeSpan<\/strong> format, I will use a standard <strong>TimeSpan<\/strong> format. The standard <strong>TimeSpan<\/strong> format strings are documented on MSDN: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee372286.aspx\">Standard TimeSpan Format Strings<\/a>. Using a standard <strong>TimeSpan<\/strong> format makes the process a lot simpler.<\/p>\n<h2>Create a custom table<\/h2>\n<p>Now that I know how to create the formatting I need for my time span, it is time to create a custom table. The trick here is to use a hash table to specify the custom property for the <strong>Format-Table<\/strong> cmdlet. Here is the hash table I use:<\/p>\n<p style=\"padding-left: 30px\">@{LABEL=&#8217;uptime&#8217;;EXPRESSION={(New-TimeSpan -start ($_.StartTime).tostring(&#8220;g&#8221;))}} -AutoSize<\/p>\n<p>The entire command is shown here. This is a single-line command that wraps over three lines in my Windows PowerShell console.<\/p>\n<p style=\"padding-left: 30px\">gps |<\/p>\n<p style=\"padding-left: 30px\">? starttime |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;select name, starttime |<\/p>\n<p style=\"padding-left: 30px\">sort starttime -descending|<\/p>\n<p style=\"padding-left: 30px\">Format-Table name,<\/p>\n<p style=\"padding-left: 30px\">@{LABEL=&#8217;uptime&#8217;;EXPRESSION={(New-TimeSpan -start ($_.StartTime).tostring(&#8220;g&#8221;))}} -AutoSize<\/p>\n<p>The command and the associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7851.HSG-3-2-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7851.HSG-3-2-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>That is it for today. Join me tomorrow for a guest blog about the Windows PowerShell community by the Scripting Wife.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a custom table that displays the process name and uptime. Microsoft Scripting Guy, Ed Wilson, is here. Sometimes it seems as if the Scripting Neighbors are crazy. Yep, it is early on a Saturday morning, and at least one of my neighbors [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[416,31,87,3,4,61,45],"class_list":["post-4082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-formatting-output","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a custom table that displays the process name and uptime. Microsoft Scripting Guy, Ed Wilson, is here. Sometimes it seems as if the Scripting Neighbors are crazy. Yep, it is early on a Saturday morning, and at least one of my neighbors [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4082"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4082\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}