{"id":1629,"date":"2009-06-16T14:46:47","date_gmt":"2009-06-16T19:46:47","guid":{"rendered":"http:\/\/www.bhargavs.com\/index.php\/powershell\/2009\/06\/measure-powershell-script-run-time\/"},"modified":"2009-06-16T14:46:47","modified_gmt":"2009-06-16T19:46:47","slug":"measure-powershell-script-run-time","status":"publish","type":"post","link":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/","title":{"rendered":"Measure script run time"},"content":{"rendered":"<p>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.<\/p>\n<p>While optimizing the code itself is not the scope of this article, what I am going to write may help you determine how long your script, function or code sections takes to run.<\/p>\n<p>Insert the following at start of the section of code you want to check:<\/p>\n<pre lang=\"PowerShell\" escaped=\"true\"># Get Start Time\n$startDTM = (Get-Date)<\/pre>\n<p>Insert the following at the end of the section of code you want to check:<\/p>\n<pre lang=\"PowerShell\" escaped=\"true\"># Get End Time\n$endDTM = (Get-Date)\n\n# Echo Time elapsed\n&quot;Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds&quot;<\/pre>\n<p>Here is an example of my code optimization:<\/p>\n<p>BEFORE (Run Time 40.4 seconds):<\/p>\n<pre lang=\"PowerShell\" escaped=\"true\">filter GetMailboxCount{\n$Server = $_\n\n$strFilter = &quot;(&amp;(&amp;(&amp; (mailnickname=*) (| (&amp;(objectCategory=person)(objectClass=user)(msExchHomeServerName=$Server)) ))))&quot;\n\n$objDomain = New-Object System.DirectoryServices.DirectoryEntry\n\n$objSearcher = New-Object System.DirectoryServices.DirectorySearcher\n$objSearcher.SearchRoot = $objDomain\n$objSearcher.PageSize = 1000\n$objSearcher.Filter = $strFilter\n$objSearcher.SearchScope = &quot;Subtree&quot;\n\n$colResults = $objSearcher.FindAll()\n$colCount = ($colResults | Measure-Object | %{$_.Count})\n&quot;$Server,$colCount&quot;\n\n}<\/pre>\n<p>AFTER (Run Time 4.53 seconds):<\/p>\n<pre lang=\"PowerShell\" escaped=\"true\">filter GetMailboxCount{\n$Server = $_\n\n$strFilter = &quot;(&amp;(&amp;(&amp; (mailnickname=*) (| (&amp;(objectCategory=person)(objectClass=user)(msExchHomeServerName=$Server)) ))))&quot;\n\n$objDomain = New-Object System.DirectoryServices.DirectoryEntry\n\n$objSearcher = New-Object System.DirectoryServices.DirectorySearcher\n$objSearcher.SearchRoot = $objDomain\n$objSearcher.PageSize = 1000\n$objSearcher.Filter = $strFilter\n$objSearcher.SearchScope = &quot;Subtree&quot;\n\n$colProplist = &quot;name&quot;\n$retval = $colProplist | foreach {$objSearcher.PropertiesToLoad.Add($_)}\n\n$colResults = $objSearcher.FindAll()\n$colCount = ($colResults | Measure-Object | %{$_.Count})\n&quot;$Server,$colCount&quot;\n\n}<\/pre>\n<p>As you will see the \u201cname\u201d property is irrelevant to my code but by querying on indexed attribute and limiting scope of the query, I was able to improve run time ten folds. Now that is huge time savings when you have to run this code in a loop across all of your servers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&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":[212],"class_list":["post-1629","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Measure script run time - 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\/2009\/06\/16\/measure-powershell-script-run-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Measure script run time - Bhargav&#039;s IT Playground\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/\" \/>\n<meta property=\"og:site_name\" content=\"Bhargav&#039;s IT Playground\" \/>\n<meta property=\"article:published_time\" content=\"2009-06-16T19:46:47+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\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/\"},\"author\":{\"name\":\"Bhargav\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/28f6d8c9b29f3a879483d65fc2ab5e26\"},\"headline\":\"Measure script run time\",\"datePublished\":\"2009-06-16T19:46:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/\"},\"wordCount\":169,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#\\\/schema\\\/person\\\/28f6d8c9b29f3a879483d65fc2ab5e26\"},\"keywords\":[\"PowerShell\"],\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/\",\"url\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/\",\"name\":\"Measure script run time - Bhargav&#039;s IT Playground\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/#website\"},\"datePublished\":\"2009-06-16T19:46:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/bhargavs.com\\\/index.php\\\/2009\\\/06\\\/16\\\/measure-powershell-script-run-time\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/bhargavs.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Measure script run time\"}]},{\"@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":"Measure script run time - 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\/2009\/06\/16\/measure-powershell-script-run-time\/","og_locale":"en_US","og_type":"article","og_title":"Measure script run time - Bhargav&#039;s IT Playground","og_description":"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 [&hellip;]","og_url":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/","og_site_name":"Bhargav&#039;s IT Playground","article_published_time":"2009-06-16T19:46:47+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\/2009\/06\/16\/measure-powershell-script-run-time\/#article","isPartOf":{"@id":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/"},"author":{"name":"Bhargav","@id":"https:\/\/bhargavs.com\/#\/schema\/person\/28f6d8c9b29f3a879483d65fc2ab5e26"},"headline":"Measure script run time","datePublished":"2009-06-16T19:46:47+00:00","mainEntityOfPage":{"@id":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/"},"wordCount":169,"commentCount":5,"publisher":{"@id":"https:\/\/bhargavs.com\/#\/schema\/person\/28f6d8c9b29f3a879483d65fc2ab5e26"},"keywords":["PowerShell"],"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/","url":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/","name":"Measure script run time - Bhargav&#039;s IT Playground","isPartOf":{"@id":"https:\/\/bhargavs.com\/#website"},"datePublished":"2009-06-16T19:46:47+00:00","breadcrumb":{"@id":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/bhargavs.com\/index.php\/2009\/06\/16\/measure-powershell-script-run-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/bhargavs.com\/"},{"@type":"ListItem","position":2,"name":"Measure script run time"}]},{"@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":34526,"url":"https:\/\/bhargavs.com\/index.php\/2024\/01\/30\/clearing-microsoft-teams-cache-with-powershell\/","url_meta":{"origin":1629,"position":0},"title":"Clearing Microsoft Teams Cache with PowerShell","author":"Bhargav","date":"January 30, 2024","format":false,"excerpt":"Introduction I recently came across a need to clear Teams cache. While the task might be simple, I ended up spending a little more time to address the scenarios I can foresee so the PowerShell script is more useful that a single use case. In this blog post, we'll be\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/bhargavs.com\/index.php\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1650,"url":"https:\/\/bhargavs.com\/index.php\/2010\/03\/29\/script-to-enable-preview-pane-for-powershell-scripts\/","url_meta":{"origin":1629,"position":1},"title":"Script to enable preview pane for PowerShell scripts","author":"Bhargav","date":"March 29, 2010","format":false,"excerpt":"If you are running Windows 7, you probably know what preview pane is. And if you use PowerShell and create ps1 scripts, you may also wonder how can you enable preview for PowerShell scripts in Windows Explorer. Well, Nate Bruneau shared how to edit registry to enable preview for ps1\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":1673,"url":"https:\/\/bhargavs.com\/index.php\/2011\/10\/21\/powershell-script-to-edit-remote-registry\/","url_meta":{"origin":1629,"position":2},"title":"PowerShell script to edit remote registry","author":"Bhargav","date":"October 21, 2011","format":false,"excerpt":"Did you ever wanted to modify your registry or add a key\/value pair to registry? Wished there was a script to help you do that? Even better, wished it can run remotely without PowerShell WinRM listener configured on target server? I had custom script that would modify certain registry entry\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":1658,"url":"https:\/\/bhargavs.com\/index.php\/2010\/12\/09\/powershell-script-to-report-uptime\/","url_meta":{"origin":1629,"position":3},"title":"PowerShell script to report uptime","author":"Bhargav","date":"December 9, 2010","format":false,"excerpt":"UPDATED - Fixed issues reporting boottime. 3\/20\/2012 I was reading Hey, Scripting Guy! article \u201cCalculating Server Uptime\u201d and decided to write a script that can do the same. While the article has very nice script that calculates uptime from event log, my script isn\u2019t fancy and simply calculates uptime since\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":1627,"url":"https:\/\/bhargavs.com\/index.php\/2009\/06\/12\/check-exchange-2003-vitals-powershell\/","url_meta":{"origin":1629,"position":4},"title":"Check Exchange 2003 vitals with PowerShell \u2013 Part II","author":"Bhargav","date":"June 12, 2009","format":false,"excerpt":"In continuation to my previous post \u201cCheck Exchange 2003 vitals with PowerShell\u201d, I also have a code block that you can replace if you want to query all exchange servers in your environment dynamically with script instead of using text file as in the code I posted earlier. In the\u2026","rel":"","context":"In &quot;Exchange 2003&quot;","block_context":{"text":"Exchange 2003","link":"https:\/\/bhargavs.com\/index.php\/category\/microsoft\/exchange-server\/exchange-2003\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1661,"url":"https:\/\/bhargavs.com\/index.php\/2011\/04\/05\/using-powershell-to-look-up-uscf-member-info\/","url_meta":{"origin":1629,"position":5},"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":[]}],"jetpack_shortlink":"https:\/\/wp.me\/pkROc-qh","_links":{"self":[{"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/posts\/1629","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=1629"}],"version-history":[{"count":0,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"wp:attachment":[{"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bhargavs.com\/index.php\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}