SOLVED

How to run Foreach loop twice

%3CLINGO-SUB%20id%3D%22lingo-sub-1656568%22%20slang%3D%22en-US%22%3EHow%20to%20run%20Foreach%20loop%20twice%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1656568%22%20slang%3D%22en-US%22%3E%3CP%3EHI%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wonder%20if%20it's%20possible%20to%20run%20a%20foreach%20loop%202%20time%2C%20for%20example%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24x%3D1..5%0A%0Aforeach%20(%24v%20in%20%24x)%20%7B%0AWrite-Host%20%24v%0Aif%20(%24foreach.current%20-eq%20%24x.Count)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20%22I%20reach%20the%20end%20and%20will%20change%20the%20values%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%24x%3D100..105%0A%20%20%20%20%20%20%20%20%20%20%20%20%24foreach.reset()%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D%0A%22End%20of%20Scripts%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Above%20script%20will%20read%20the%20value%20from%20X%20which%20is%201%20till%205%2C%20and%20do%20some%20logic%20in%20the%20Foreach%20loop%2C%20But%20what%20I%20am%20stuck%20in%20is%20after%20all%20the%20execution%20of%20Foreach%20finished%2C%20I%20want%20to%20re-execute%20the%20same%20logic%20but%20with%20a%20different%20value%20of%20%24X.%3C%2FP%3E%3CP%3EAny%20Idea%20about%20this.%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1656568%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1664070%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20Foreach%20loop%20twice%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1664070%22%20slang%3D%22en-US%22%3EHi%2C%20you%20should%20first%20name%20correctly%20your%20variables%2C%20and%20indent%20your%20script.%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach%20(%24item%20in%20%24list)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%2F%2Fyour%20method%3CBR%20%2F%3Eif%20(%24item%20-eq%20%24list.Count)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20%22I%20reach%20the%20end%20and%20will%20change%20the%20values%22%3CBR%20%2F%3E%24list%3D100..105%3CBR%20%2F%3Eforeach%20(%24item%20in%20%24list)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%2F%2Fyour%20method%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20clearly%20not%20the%20best%20way%20to%20do%2C%20but%20it%20works%20for%20two%20value%20of%20%24list.%20In%20fact%2C%20there%20is%20two%20better%20solutions%20%3A%3CBR%20%2F%3E%3CBR%20%2F%3E-%20encapsulate%20your%20method%20and%20run%20it%20twice%20directly.%3CBR%20%2F%3E-%20trying%20to%20do%20a%20%22recursive%20method%22%2C%20which%20is%20quite%20complex.%3CBR%20%2F%3E%3CBR%20%2F%3EIt%20depends%20on%20your%20need%2C%20but%20keep%20in%20mind%20that%20recursive%20method%20is%20not%20%22finger%20in%20the%20nose%22.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1668114%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20Foreach%20loop%20twice%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1668114%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F792769%22%20target%3D%22_blank%22%3E%40arnaudhelin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%26nbsp%3B%3C%2FP%3E%3CP%3ESurely%20I%20use%20a%20better%20Var%20names%2C%20but%20here%20just%20to%20make%20it%20short%20and%20easy.%3C%2FP%3E%3CP%3Eanyway%2C%20the%20method%20you%20propose%20will%20still%20need%20to%20run%20a%202%20foreach%2C%20I%20want%20to%20do%20it%20using%201%20foreach%20loop%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

HI, 

I wonder if it's possible to run a foreach loop 2 time, for example

 

 

$x=1..5

foreach ($v in $x) {
Write-Host $v
if ($foreach.current -eq $x.Count){
            Write-Host "I reach the end and will change the values"
            $x=100..105
            $foreach.reset()}
              
}
"End of Scripts"

 

 

The Above script will read the value from X which is 1 till 5, and do some logic in the Foreach loop, But what I am stuck in is after all the execution of Foreach finished, I want to re-execute the same logic but with a different value of $X.

Any Idea about this.

Thanks

 

3 Replies
Hi, you should first name correctly your variables, and indent your script.

foreach ($item in $list)
{
//your method
if ($item -eq $list.Count)
{
Write-Host "I reach the end and will change the values"
$list=100..105
foreach ($item in $list)
{
//your method
}
}
}

This clearly not the best way to do, but it works for two value of $list. In fact, there is two better solutions :

- encapsulate your method and run it twice directly.
- trying to do a "recursive method", which is quite complex.

It depends on your need, but keep in mind that recursive method is not "finger in the nose".

@arnaudhelin 

Thanks, 

Surely I use a better Var names, but here just to make it short and easy.

anyway, the method you propose will still need to run a 2 foreach, I want to do it using 1 foreach loop

Thanks

 

best response confirmed by farism (Occasional Contributor)
Solution

@farism 

Use Function instead

place the Business logic in a function and pass the value to it through the code.