{"id":1660,"date":"2011-04-04T07:56:23","date_gmt":"2011-04-04T15:56:23","guid":{"rendered":"http:\/\/www.bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/"},"modified":"2011-04-04T07:56:23","modified_gmt":"2011-04-04T15:56:23","slug":"how-do-i-measure-how-long-a-command-took-to-run-in-powershell","status":"publish","type":"post","link":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/","title":{"rendered":"How do I measure how long a command took to run in PowerShell?"},"content":{"rendered":"<p>So this one might be pretty simple you say, just use measure-command right? But what if I already ran the command and didn\u2019t time it? What if it took really long time and running it again isn\u2019t an option?<\/p>\n<p>This is when built-in history helps! If you run <\/p>\n<pre class=\"brush: ps\">Get-History | Get-Member<\/pre>\n<p>You will see the following: <\/p>\n<pre class=\"brush: ps\">   TypeName: Microsoft.PowerShell.Commands.HistoryInfo\n\nName               MemberType Definition\n----               ---------- ----------\nClone              Method     Microsoft.PowerShell.Commands.HistoryInfo Clone()\nEquals             Method     bool Equals(System.Object obj)\nGetHashCode        Method     int GetHashCode()\nGetType            Method     type GetType()\nToString           Method     string ToString()\nCommandLine        Property   System.String CommandLine {get;}\nEndExecutionTime   Property   System.DateTime EndExecutionTime {get;}\nExecutionStatus    Property   System.Management.Automation.Runspaces.PipelineState ExecutionStatus {get;}\nId                 Property   System.Int64 Id {get;}\nStartExecutionTime Property   System.DateTime StartExecutionTime {get;}<\/pre>\n<p>Notice the properties \u201cStartExecutionTime\u201d and \u201cEndExecutionTime\u201d. For every command kept in PowerShell history, it keeps track of when it started and when it ended. To see how long it took to run the command, you can just use built-in math functionality. Here is how:<\/p>\n<pre class=\"brush: ps\">$h = get-history\n$h[-1].EndExecutionTime - $h[-1].StartExecutionTime<\/pre>\n<p>The output would look something like this:<\/p>\n<pre class=\"brush: ps\">Days              : 0\nHours             : 0\nMinutes           : 0\nSeconds           : 0\nMilliseconds      : 46\nTicks             : 467997\nTotalDays         : 5.41663194444444E-07\nTotalHours        : 1.29999166666667E-05\nTotalMinutes      : 0.000779995\nTotalSeconds      : 0.0467997\nTotalMilliseconds : 46.7997<\/pre>\n<p>This also applies to your scripts. If you ran script, reported time will be how long script took to execute. It does not keep track of individual commands contained within your script but you already knew that, didn\u2019t you?<\/p>\n<p>Originally posted at http:\/\/blogs.technet.com\/bshukla<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So this one might be pretty simple you say, just use measure-command right? But what if I already ran the command and didn\u2019t time it? What if it took really [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19],"tags":[],"class_list":["post-1660","post","type-post","status-publish","format-standard","hentry","category-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How do I measure how long a command took to run in PowerShell? - Bhargav&#039;s IT Playground<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I measure how long a command took to run in PowerShell? - Bhargav&#039;s IT Playground\" \/>\n<meta property=\"og:description\" content=\"So this one might be pretty simple you say, just use measure-command right? But what if I already ran the command and didn\u2019t time it? What if it took really [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"Bhargav&#039;s IT Playground\" \/>\n<meta property=\"article:published_time\" content=\"2011-04-04T15:56:23+00:00\" \/>\n<meta name=\"author\" content=\"Bhargav\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bhargav\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/\"},\"author\":{\"name\":\"Bhargav\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/28f6d8c9b29f3a879483d65fc2ab5e26\"},\"headline\":\"How do I measure how long a command took to run in PowerShell?\",\"datePublished\":\"2011-04-04T15:56:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/\"},\"wordCount\":167,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/28f6d8c9b29f3a879483d65fc2ab5e26\"},\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/\",\"url\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/\",\"name\":\"How do I measure how long a command took to run in PowerShell? - Bhargav&#039;s IT Playground\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#website\"},\"datePublished\":\"2011-04-04T15:56:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2011\\\/04\\\/04\\\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/bhargavs.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I measure how long a command took to run in PowerShell?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#website\",\"url\":\"https:\\\/\\\/bhargavs.com\\\/\",\"name\":\"Bhargav's IT Playground\",\"description\":\"Passion for Technology. Power of Collaboration.\",\"publisher\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/28f6d8c9b29f3a879483d65fc2ab5e26\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/bhargavs.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/28f6d8c9b29f3a879483d65fc2ab5e26\",\"name\":\"Bhargav\",\"logo\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/bhargavs.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do I measure how long a command took to run in PowerShell? - Bhargav&#039;s IT Playground","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/","og_locale":"en_US","og_type":"article","og_title":"How do I measure how long a command took to run in PowerShell? - Bhargav&#039;s IT Playground","og_description":"So this one might be pretty simple you say, just use measure-command right? But what if I already ran the command and didn\u2019t time it? What if it took really [&hellip;]","og_url":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/","og_site_name":"Bhargav&#039;s IT Playground","article_published_time":"2011-04-04T15:56:23+00:00","author":"Bhargav","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Bhargav","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/#article","isPartOf":{"@id":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/"},"author":{"name":"Bhargav","@id":"https:\/\/bhargavs.com\/#\/schema\/person\/28f6d8c9b29f3a879483d65fc2ab5e26"},"headline":"How do I measure how long a command took to run in PowerShell?","datePublished":"2011-04-04T15:56:23+00:00","mainEntityOfPage":{"@id":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/"},"wordCount":167,"commentCount":0,"publisher":{"@id":"https:\/\/bhargavs.com\/#\/schema\/person\/28f6d8c9b29f3a879483d65fc2ab5e26"},"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/","url":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/","name":"How do I measure how long a command took to run in PowerShell? - Bhargav&#039;s IT Playground","isPartOf":{"@id":"https:\/\/bhargavs.com\/#website"},"datePublished":"2011-04-04T15:56:23+00:00","breadcrumb":{"@id":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/bhargavs.com\/index.php\/2011\/04\/04\/how-do-i-measure-how-long-a-command-took-to-run-in-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/bhargavs.com\/"},{"@type":"ListItem","position":2,"name":"How do I measure how long a command took to run in PowerShell?"}]},{"@type":"WebSite","@id":"https:\/\/bhargavs.com\/#website","url":"https:\/\/bhargavs.com\/","name":"Bhargav's IT Playground","description":"Passion for Technology. Power of Collaboration.","publisher":{"@id":"https:\/\/bhargavs.com\/#\/schema\/person\/28f6d8c9b29f3a879483d65fc2ab5e26"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/bhargavs.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/bhargavs.com\/#\/schema\/person\/28f6d8c9b29f3a879483d65fc2ab5e26","name":"Bhargav","logo":{"@id":"https:\/\/bhargavs.com\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/bhargavs.com"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1201,"url":"https:\/\/bhargavs.com\/index.php\/2015\/12\/22\/replace-command-prompt-with-powershell-in-windows-10-winx-menu\/","url_meta":{"origin":1660,"position":0},"title":"Replace Command Prompt with PowerShell in Windows 10 Win+X menu","author":"Bhargav","date":"December 22, 2015","format":false,"excerpt":"I\u2019m sure this has been written about many times in many places but whenever I install Windows 8.1 or Windows 10 on my machines\/VMs, I seem to keep forgetting so this would be a good way to remember. If you are reading this and benefit from it, even better! When\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/windows\/"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/bhargavs.com\/wp-content\/uploads\/2015\/12\/image_thumb-8.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1629,"url":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/","url_meta":{"origin":1660,"position":1},"title":"Measure script run time","author":"Bhargav","date":"June 16, 2009","format":false,"excerpt":"When I am writing PowerShell scripts, one thing that is always in my mind: How long will it take to run this script and what can I do to reduce the run time. While optimizing the code itself is not the scope of this article, what I am going to\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1647,"url":"https:\/\/bhargavs.com\/index.php\/2010\/03\/16\/send-a-tweet-from-powershell\/","url_meta":{"origin":1660,"position":2},"title":"Send a Tweet from PowerShell","author":"Bhargav","date":"March 16, 2010","format":false,"excerpt":"While I chose to keep this article here for archiving, Please note that this doesn't work anymore. Twitter API has since changed and what used to work, doesn't anymore. I was reading Shay\u2019s article on how to send direct messages on twitter using PowerShell when I couldn\u2019t resist but find\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/powershell\/"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.bhargavs.com\/wp-content\/uploads\/2010\/03\/image-thumb.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.bhargavs.com\/wp-content\/uploads\/2010\/03\/image-thumb.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.bhargavs.com\/wp-content\/uploads\/2010\/03\/image-thumb.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1661,"url":"https:\/\/bhargavs.com\/index.php\/2011\/04\/05\/using-powershell-to-look-up-uscf-member-info\/","url_meta":{"origin":1660,"position":3},"title":"Using PowerShell to look up USCF member info","author":"Bhargav","date":"April 5, 2011","format":false,"excerpt":"If you are one of the chess players who play rated USCF (United States Chess Federation) games, you may have used their look up tool to find someone\u2019s rating and other details, or may be your own after you completed a tournament. Being a PowerShell geek, I had to figure\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":131,"url":"https:\/\/bhargavs.com\/index.php\/2009\/06\/17\/powershell-get-service-startuptype\/","url_meta":{"origin":1660,"position":4},"title":"Get-Service StartupType","author":"Bhargav","date":"June 17, 2009","format":false,"excerpt":"If you want to find out startup type of a service using Get-Service cmdlet, you are out of luck. Get-Service cmdlet does not have StartupType property. Set-Service however does. So how can you find out the startup type of a service using powershell? You can use Get-WmiObject. (Get-WmiObject Win32_Service -filter\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":81,"url":"https:\/\/bhargavs.com\/index.php\/2009\/05\/28\/exchange-2007-powershell-force-mount-database\/","url_meta":{"origin":1660,"position":5},"title":"Force Mount-Database","author":"Bhargav","date":"May 28, 2009","format":false,"excerpt":"I wanted to mount All mailbox databases from all servers that had them. This was a scenario where I had servers recovered with no DB files. I did not need them, I was going to start with empty databases. The command was going to be simple: Get-MailboxDatabase | Mount-Database -Confirm:$false\u2026","rel":"","context":"In &quot;Exchange 2007&quot;","block_context":{"text":"Exchange 2007","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/exchange-server\/exchange-2007\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_shortlink":"https:\/\/wp.me\/pkROc-qM","_links":{"self":[{"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/posts\/1660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/comments?post=1660"}],"version-history":[{"count":0,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/posts\/1660\/revisions"}],"wp:attachment":[{"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/media?parent=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/categories?post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/tags?post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}