{"id":651,"date":"2023-09-13T15:47:00","date_gmt":"2023-09-13T15:47:00","guid":{"rendered":"http:\/\/draith.com\/?p=651"},"modified":"2024-02-08T01:52:26","modified_gmt":"2024-02-08T01:52:26","slug":"cranking-up-the-efficiency-optimizing-powershell-scripts","status":"publish","type":"post","link":"http:\/\/draith.com\/?p=651","title":{"rendered":"Cranking Up the Efficiency: Optimizing PowerShell Scripts"},"content":{"rendered":"\n<p>Hey there, PowerShell aficionados! Whether you&#8217;re automating your morning coffee or deploying a fleet of VMs into the cloud, efficiency is key. Nobody wants to watch paint dry while their script runs in the background. So, let&#8217;s put some pep into that PowerShell script of yours. We&#8217;re diving straight into the realm of optimization \u2013 no fluff, just the good stuff.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Measure, Then Cut: Profiling Your Script<\/h2>\n\n\n\n<p>Before you start tweaking, let&#8217;s figure out where the bottlenecks are. PowerShell, being the Swiss Army knife it is, comes equipped with some nifty profiling tools like <code>Measure-Command<\/code>. This cmdlet lets you time how long it takes for a script or command to run. Use it to identify slow parts of your script:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">Measure-Command { .\\YourScript.ps1 }<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Lean and Mean: Streamlining Execution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Filter Left, Format Right<\/strong><\/h3>\n\n\n\n<p>One of the golden rules for optimizing PowerShell scripts is to do your filtering as early as possible. Use cmdlets like <code>Where-Object<\/code> and <code>Select-Object<\/code> judiciously to trim down your data before processing it further. Remember, processing less data means faster execution:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">Get-Process | Where-Object { $_.CPU -gt 100 } | Select-Object Name, CPU<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Avoid the Pipeline When Possible<\/strong><\/h3>\n\n\n\n<p>While the pipeline is one of PowerShell&#8217;s most powerful features, it&#8217;s not always the most efficient. Each pipe operation adds overhead. For tight loops or operations that need to be as fast as possible, consider using .NET collections or array manipulations:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$processes = Get-Process\n$highCpuProcesses = [System.Collections.ArrayList]@()\nforeach ($process in $processes) {\n    if ($process.CPU -gt 100) {\n        [void]$highCpuProcesses.Add($process)\n    }\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Use Foreach-Object Carefully<\/strong><\/h3>\n\n\n\n<p><code>Foreach-Object<\/code> is versatile but can be slower than its <code>foreach<\/code> loop counterpart due to pipeline overhead. For large datasets, stick to <code>foreach<\/code> for better performance:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\"># Slower\nGet-Process | Foreach-Object { $_.Kill() }\n\n# Faster\nforeach ($process in Get-Process) {\n    $process.Kill()\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Need for Speed: Parallel Processing<\/h2>\n\n\n\n<p>When you&#8217;re dealing with tasks that can be run concurrently, PowerShell 7&#8217;s <code>ForEach-Object -Parallel<\/code> can be a game-changer. This allows you to run multiple operations at the same time, significantly speeding up processes:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">1..10 | ForEach-Object -Parallel { Start-Sleep -Seconds $_; &quot;Slept for $_ seconds&quot; } -ThrottleLimit 10<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">A Parting Tip: Stay Up-to-Date<\/h2>\n\n\n\n<p>PowerShell and .NET are constantly evolving, with new features and performance improvements being added regularly. Make sure your PowerShell version is up-to-date to take advantage of these enhancements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrap-Up<\/h2>\n\n\n\n<p>Optimizing PowerShell scripts can turn a sluggish sequence of commands into a streamlined process that runs at lightning speed. By measuring performance, refining your approach, and employing parallel processing, you can ensure your scripts are not only efficient but also maintainable. Happy scripting, and may your execution times always be minimal!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there, PowerShell aficionados! Whether you&#8217;re automating your morning coffee or deploying a fleet of VMs into the cloud, efficiency is key. Nobody wants to watch paint dry while their script runs in the background. So, let&#8217;s put some pep into that PowerShell script of yours. We&#8217;re diving straight into the realm of optimization \u2013 &hellip; <a href=\"http:\/\/draith.com\/?p=651\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cranking Up the Efficiency: Optimizing PowerShell Scripts&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16],"class_list":["post-651","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=651"}],"version-history":[{"count":1,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":652,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/651\/revisions\/652"}],"wp:attachment":[{"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}