Author Posts

October 3, 2016 at 12:52 pm

I have a Foreach loop that ultimately returns thousands upon thousands of results. I'm having to tweak the output information to get just what I want in the output/results, so having to rerun it multiple times to test the output format and content. I'd like to keep my existing code, but just return the first 100 results.

What's the simplest method to run through the loop and either exit out or simply stop the loop at 100? Something that I can easily remove or comment out once I dial in the output. I've been piping the output to the screen or file, stopping it after 5 or so seconds, then reviewing the content... but would like to just have it stop at 100.

I thought there was a switch (or possibly another command) that would allow me something like...

foreach ($user{100} in $users)

...or something like that, indicating to stop the loop at 100.


October 3, 2016 at 1:15 pm

foreach ($user in ($users | Select-Object -First 100) )

That ought to do it.

October 3, 2016 at 2:37 pm

You can use index.

foreach ($user in $users[0..99]){Code for each $user}

October 3, 2016 at 2:43 pm

That's more like what I had in mind... Thanks "random Commandline".

October 3, 2016 at 2:44 pm

Craig, that works too! I like it. Thanks.