Powered By Blogger

Aug 8, 2012


PowerShell - Foreach Syntax


In PowerShell there are two ways to use the foreach looping technique. This post briefly describes those two ways. 

 foreach ($object in $collection) {something to do with the $object}

This is a very simple technique. A good example would be get a set of directories into a variable and then put that variable into a foreach loop to control the output.



$collection | foreach {do something $_}

By using this technique we can pipeline the collection to the foreach command can use the scriptblock with the pipelined ($_). The previous example can be done using this technique in one line. 



I hope this small description provides an idea about the differences between the two techniques. 

No comments:

Post a Comment