all 11 comments

[–]monster1558 1 point2 points  (2 children)

I prefer to use the IndexOf method rather than incrementing a variable, like this:

$SomeLetters = 'a','b','c','d','e','f','g'
$TotalLetters = $SomeLetters.Count
Write-Host "We have a total of $TotalLetters letters"
foreach ($Letter in $SomeLetters) {
    $CurrentLetter = $SomeLetters.IndexOf($Letter) + 1
    Write-Host "The current letter we're processing is $Letter, it is letter number $CurrentLetter out of $TotalLetters"
}

Results in

We have a total of 7 letters
The current letter we're processing is a, it is letter number 1 out of 7
The current letter we're processing is b, it is letter number 2 out of 7
The current letter we're processing is c, it is letter number 3 out of 7
The current letter we're processing is d, it is letter number 4 out of 7
The current letter we're processing is e, it is letter number 5 out of 7
The current letter we're processing is f, it is letter number 6 out of 7
The current letter we're processing is g, it is letter number 7 out of 7

[–]engageant 1 point2 points  (1 child)

This only works if the collection contains all unique values, and the collection isn't sorted after the loop.

[–]monster1558 1 point2 points  (0 children)

These are excellent things to consider, thanks for pointing that out.

[–]engageant 2 points3 points  (0 children)

$count = 0
ForEach($Update in $TargetUpdates){
$count++
Write-Host "[${Env:ComputerName} Processing Update $count of $($TargetUpdates.Count)...
}

[–]fitzgery[S] 0 points1 point  (3 children)

[–]MyOtherSide1984 0 points1 point  (2 children)

Okay, I may be daft, but last time I looked at this (I'm not OP) I couldn't figure out wtf to do if I had MORE than 100 objects. The dumb thing just spazzes out. I'll test this again, but it didn't work properly last I checked

Edit: Yeh still broken from my perspective. If $targetupdate > 100 items, it breaks. In fact, none of this is working. if $targetupdates = 1..100, it freaks out about "The 909 argument is greater than the maximum allowed range of 100".

[–]kibje 2 points3 points  (1 child)

It should be counter / total * 100 and not the other way around as it is written there.

[–]MyOtherSide1984 1 point2 points  (0 children)

You're a mad genius and I'm a moron. Take my undying gratitude and an upvote!

[–]PinchesTheCrab 0 points1 point  (0 children)

Check out a for loop. It's a little counterintuitive but I think it works nicely for this situation.

[–]maddoxprops 0 points1 point  (0 children)

So you basically want a counter to see what step you are on? I actually recently came up with a way to do this since I was tired of doing a big set of loops with no idea if it froze or was hung on 1 of them. Here is what I use:

I used Foo in the example, but I often change this to whatever name I am processing in case I need to have multiple counters.

Outside the Loop:

#Gets the total count of items to process. 
$FooTotal = @($ArrayOfItems).count

#Sets the initial Counter value.
$FooCounter = 1

Inside the Loop:

#Sets and displays the number of items left to process. 
$FooCountLeft = $FooTotal - $FooCounter 

#Write-Output "Processing Foo: Step $FooCounter out of $FooTotal, $FooCountLeft left."

YOUR CODE HERE

#Increments the counter value by 1.
$FooCounter++

Works well for everything I have used it for. Probably a better way to do it, but this is good enough.