{"id":2100,"date":"2014-01-31T11:59:00","date_gmt":"2014-01-31T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/31\/powertip-learn-to-use-where-or-foreach-in-powershell-4-0\/"},"modified":"2014-01-31T11:59:00","modified_gmt":"2014-01-31T11:59:00","slug":"powertip-learn-to-use-where-or-foreach-in-powershell-4-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-learn-to-use-where-or-foreach-in-powershell-4-0\/","title":{"rendered":"PowerTip: Learn to Use Where or ForEach in PowerShell 4.0"},"content":{"rendered":"<p><b>Summary<\/b>: Learn the easy way to use <strong>Where<\/strong> or <strong>ForEach<\/strong> in Windows PowerShell 4.0.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;I am running Windows PowerShell&nbsp;4.0 and I want to know the easy way to use&nbsp;<b>Where<\/b>&nbsp;or&nbsp;<b>ForEach<\/b>.<\/p>\n<p style=\"margin-left:30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;In Windows PowerShell&nbsp;4.0, object arrays have a built-in&nbsp;<b>.Where()&nbsp;<\/b>and&nbsp;<b>.ForEach()<\/b>&nbsp;methods. <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; You can use these methods to filter objects or perform operations with each object in the array.<\/p>\n<p style=\"margin-left:60px\">For example, to use the&nbsp;<b>Where()<\/b>&nbsp;method to filter for a specific process name, run this command:<\/p>\n<p style=\"margin-left:120px\">@(Get-Process).Where({ $PSItem.Name -eq &#8216;notepad&#8217;; }).<\/p>\n<p style=\"margin-left:60px\">To iterate over a list of objects in an array and perform an operation against them by using the&nbsp;<b>ForEach()<\/b>&nbsp;method, run this command:<\/p>\n<p style=\"margin-left:120px\">@(Get-Process &ndash;Name notepad).ForEach({ Stop-Process -InputObject $PSItem; }).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn the easy way to use Where or ForEach in Windows PowerShell 4.0. &nbsp;I am running Windows PowerShell&nbsp;4.0 and I want to know the easy way to use&nbsp;Where&nbsp;or&nbsp;ForEach. &nbsp;In Windows PowerShell&nbsp;4.0, object arrays have a built-in&nbsp;.Where()&nbsp;and&nbsp;.ForEach()&nbsp;methods. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; You can use these methods to filter objects or perform operations with each [&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":[356,3,45],"class_list":["post-2100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powertip","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn the easy way to use Where or ForEach in Windows PowerShell 4.0. &nbsp;I am running Windows PowerShell&nbsp;4.0 and I want to know the easy way to use&nbsp;Where&nbsp;or&nbsp;ForEach. &nbsp;In Windows PowerShell&nbsp;4.0, object arrays have a built-in&nbsp;.Where()&nbsp;and&nbsp;.ForEach()&nbsp;methods. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; You can use these methods to filter objects or perform operations with each [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2100","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=2100"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2100\/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=2100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}