Foreach-object v/s Foreach (Powershell)

Dhrub Bharali
Oct 24, 2020

--

Looping is a core of any code or script and we have to make sure we have to do it correctly. One interesting question everyone asks which is a better loop when you want to run a specific command in several computers and whole heartedly we choose Foreach() loop. Foreach is one of the rising star when chosen between invoke-commad and where { }. Advantage of having foreach is that we can store value under one string in excel which is a boon. Likewise we get 2 different option while choosing foreach () , 1 is foreach itself and other is foreach-object. But when you see the object it looks similar doesn’t it. Well I have performed one test so that we can pick our winner.

Let’s dive into it…

$a=Get-ChildItem –File C:\users\dhrub\ -Recurse

$time = (Measure-Command {

$a | ForEach-Object {

$_

}

Want to know the Result Click here — >

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dhrub Bharali
Dhrub Bharali

Written by Dhrub Bharali

0 Followers

Full time Powershell script writer and half time gamer!!

No responses yet

Write a response