I have an array with strings:
$Names = @("Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10")
I want to do the following:
I want to have a Skip variable, where I will cut my array in chunks, and want to print my names with 5 seconds in between.
I have tried it by doing the following:
$BatchSkip = 5
for($i = 0; $i -lt $names.Count; $i += $BatchSkip){
$tempArray = $names |Select-Object -First $BatchSkip
foreach ($name in $tempArray)
{
write-host $name
} Start-Sleep -Seconds 5
}
But this keeps printing the same names. What factor is missing in my code to implement that it ‘loops’ over my array?
>Solution :
If I correctly understood what you are trying to achieve, then this should work:
$Names = @("Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10")
$BatchSkip = 5
for($i = 0; $i -lt $names.Count; $i += $BatchSkip){
$tempArray = $names[$i..($i+$BatchSkip-1)]
foreach ($name in $tempArray)
{
write-host $name
} Start-Sleep -Seconds 5
}
Your tempArray did not contain the correct values of names, since there was no dependence on the loop variable i.