Josef Z.'s helpful answer shows a viable solution.
Your specific desire to get a progress bar simply by inserting a Write-Progress command into your existing command's ForEach-Object script block is impossible, however:
The script block passed to ForEach-Object has no way of knowing in advance how many objects will be passed through the pipeline, which is a prerequisite for showing progress in terms of percentages.
You must determine the iteration count ahead of time.
If you want to generalize this, use a function, such as the following (purposely kept simple):
function Invoke-WithProgress {
param(
[scriptblock] $Process,
[string] $Activity = 'Processing'
)
# Collect the pipeline input ($Input) up front in an array,
# using @(...) to force enumeration.
$a = @($Input)
# Count the number of input objects.
$count = $a.Count; $i = 0
# Process the input objects one by one, with progress display.
$a | ForEach-Object {
Write-Progress -PercentComplete ((++$i)*100/$count) -Activity "$Activity $i/$count..."
. $Process
}
}
Again, note that all pipeline input is collected up front.
In your case, you'd invoke it as follows:
1..254 | Invoke-WithProgress {
Test-Connection -ErrorAction SilentlyContinue -count 1 -TimeToLive 32 "$ipcut.$_"
}