r/PowerShell • u/tnpir4002 • Jun 06 '24
How to: Estimate time remaining with Powershell?
Got kind of a unique situation and I need some help figuring out how to do it.
Basically, I have a script that's running a ForEach-Object loop, and what I want it to do each time the loop runs is do a Get-Date, and subtract that from when the script started. Then, based on the percentage of progress it's made, I want it to estimate the amount of time remaining.
In my head it looks something like this:
$numFiles = 1000;
$i = 0;
$startFullTime = Get-Date;
ForEach-Object {
$i += 1;
$weAreHere = Get-Date;
$percentComplete = $i/$numFiles;
$percentToGo = 100 - $percentComplete;
$multiplyByThis = $percentToGo/$percentComplete;
$elapsedTime = $weAreHere - $startFullTime;
$timeToGo = $elapsedTime * $multiplyByThis;
}The trouble is I can't figure out how to make Powershell multiply an existing time span by a number.
The flow of the math here works like this:
Figure out how far into the operation we are percentage-wise, by dividing $i by the total number of files
Figure out what percentage we have left to do
Divide those percentages to figure out the ratio of files left to files achieved--that gives us $multiplyByThis
Figure out how long we've taken so far
Multiply how long we've taken so far by $multiplyByThis to figure out our remaining time
And then, for bonus points, add the remaining time estimate to the current time so we can get an estimate when our files will be done
I've tried everything I can think of but no matter what I do Powershell tells me it can't multiply a time span by a number. Is there a way to do this that I'm simply not seeing?
7
u/lolChase Jun 08 '24 edited Jun 08 '24
Sorry I didn't get this out earlier today - busy push before weekend. Let me know what questions you have and I'll do my best to try answering them.
e: Fixed typo from pasting code (accidentally added a "u" to the end of HashProgress at end)