{"id":1303,"date":"2011-11-08T17:46:57","date_gmt":"2011-11-08T16:46:57","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1303"},"modified":"2015-09-08T20:36:55","modified_gmt":"2015-09-08T18:36:55","slug":"powershell-foreach-vs-foreach-object","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/powershell-foreach-vs-foreach-object\/","title":{"rendered":"PowerShell Foreach vs ForEach-Object"},"content":{"rendered":"<p>There are two variants of &#8216;for each&#8217; in PowerShell:<br \/>\nForeach statement: iterates over a collection of objects<br \/>\nForEach-Object: obtains its entries from the pipeline<\/p>\n<p>At first they both seemed to do the job, but there are some differences.<br \/>\nLet&#8217;s do a test with a simple array of items and loop through it using both foreach methods. To measure the time elapsed to loop the array the Measure-Command is used.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\n\r\n$items = 1,2,3,4,5,6,7,8,9,0,10,12,13,14,15,52,58,41,59,841,5,4,1\r\n\r\nWrite-Host &quot;ForEach-Object: &quot;\r\n(Measure-Command {\u00a0 `\r\n\u00a0$items | ForEach-Object { `\r\n\u00a0\u00a0&quot;Item: $_&quot; `\r\n\u00a0}}).totalmilliseconds\r\n\r\nWrite-Host &quot;Foreach: &quot;\r\n(Measure-Command {\u00a0 `\r\n\u00a0Foreach ($item in $items) { `\r\n\u00a0\u00a0&quot;Item: $element&quot; `\r\n\u00a0}}).totalmilliseconds\r\n\r\n<\/pre>\n<p>Multiple\u00a0results:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/ForEach.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1306\" title=\"ForEach\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/ForEach.png\" alt=\"\" width=\"130\" height=\"407\" \/><\/a><\/p>\n<p>The results differ a lot!<\/p>\n<h3>\nWhen to use which method?<\/h3>\n<p>When the items of the array are known at forehand, like in the test, foreach is the better approach because of the speed. This kind of speed can only be reached when all the objects are already known and stored in a variable.<\/p>\n<p>ForEach-Object is a different story. When the items have to be collected before the loop starts and this can take a while: use ForEach-Object.<br \/>\nThis method processes the objects collected in the array directly when available and doesn&#8217;t wait until all objects are collected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two variants of &#8216;for each&#8217; in PowerShell: Foreach statement: iterates over a collection of objects ForEach-Object: obtains its entries from the pipeline At first they both seemed to do the job, but there are some differences. Let&#8217;s do &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/powershell-foreach-vs-foreach-object\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,8],"tags":[43],"class_list":["post-1303","post","type-post","status-publish","format-standard","hentry","category-powershell","category-sharepoint-2010","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1303"}],"version-history":[{"count":6,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1303\/revisions"}],"predecessor-version":[{"id":1310,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1303\/revisions\/1310"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}