Ending A Powershell Loop

Steve Jones shows how to end a loop early:

That’s fine, but it’s not a great loop. It runs 9999 times, which isn’t what I want. It works, but it’s an unnecessary use of resources. However I don’t want to break the loop when the file file isn’t found. There have been issues generating a file, like #350, when #351 exists and is there.

I decided to use a shortcut technique I had learned as a kid. I set a variable and then incremented it when I missed a file. When the increment reaches some value, I break the loop.

I’d just as soon use a break statement, but there are many ways to skin a cat.

Related Posts

Scoping And Powershell Script Blocks

Shane O’Neill looks at variable scope within script blocks in Powershell: Now what happens if we don’t want to use switch? What happens if we tried to do this with script blocks instead? I’ve created 3 different script blocks here just for ease of use. If you want to create a single script block to […]

Read More

Concatenating Multiple SQL Files

Steve Stedman has a quick Powershell one-liner to concatenate multiple files: I come across the need occasionally to deploy a set of sql files that are all checked into source control in different files with a file hierarchy like this: Database Name Type of object (proc, table, view, etc) Name of object When I go […]

Read More


December 2015
« Nov Jan »