Hi together,

How can I make a countdown that shows how much time remains until the $Time (next backup) is made?

For example the every 30 minutes the script tests if a change was made then make a backup or not I would like to see how many minutes it takes to make the next test.

timer should be here at " $Timer ": Write-Host “no change - test number = [$($i)] $DateFormatSystem $Timer nn”

Thanks for any help!

for ($i = 1; $i -lt 5001; $i++) {

# This does not work
$Timer = ($Time /60) 
for ($Timer = 30; $Timer -gt 0; $Timer--) {
  Write-Host -NoNewLine "`r$Timer"
  Start-Sleep -Milliseconds 1000
}
Write-Host #ends the line after loop

    $Now = Get-Date -Format $DateFormat
    $DateFormatSystem = Get-Date -Format "(dd.MM.yyyy HH:mm:ss)"
    $Folder = Join-Path -Path $Destination -ChildPath ($Now + (Split-Path -Path $FolderName -Leaf))
    if (Get-ChildItem -Path $Source -Recurse -Force | Where-Object { $_.Attributes -match 'Archive' }) {
        Write-Host "Backup made - test number = ($($i)) $DateFormatSystem`n`n" -ForegroundColor Green
        robocopy $Source $Folder /E /XC /XN /XO 
        Push-Location -Path $Source
        attrib -A /S /D 
        Pop-Location
  } else {
    Write-Host "no change - test number = [$($i)] $DateFormatSystem $Timer`n`n" -ForegroundColor Red
    } 
    Start-Sleep -Seconds $Time
}
4 Spice ups

Something like this?

# How long to wait between processes in Minutes
$Time = 5

for ($i = 1; $i -lt 5001; $i++) {

# This does not work
# $Timer = ($Time /60) 

for ($Timer = $Time*60; $Timer -gt 0; $Timer--) {
  $Endtime = Get-date
  $Endtime = ($Endtime.AddSeconds($timer)).ToShortTimeString()
  # Write-Host -NoNewLine "`r$Timer  $Endtime"
  Start-Sleep -Milliseconds 1000
  Write-Progress -Activity "Backup" -Status "Waiting for next window at $Endtime" -PercentComplete  -1 -SecondsRemaining $Timer
}
Write-Progress -Completed -Activity "Backup" -Status "Waiting for next window at $Endtime" -PercentComplete  -1 -SecondsRemaining $Timer
# Write-Host #ends the line after loop

    $Now = Get-Date -Format $DateFormat
    $DateFormatSystem = Get-Date -Format "(dd.MM.yyyy HH:mm:ss)"

    $Folder = Join-Path -Path $Destination -ChildPath ($Now + (Split-Path -Path $FolderName -Leaf))
    if (Get-ChildItem -Path $Source -Recurse -Force | Where-Object { $_.Attributes -match 'Archive' }) {
        Write-Host "Backup made - test number = ($($i)) $DateFormatSystem`n`n" -ForegroundColor Green
        robocopy $Source $Folder /E /XC /XN /XO 
        Push-Location -Path $Source
        attrib -A /S /D 
        Pop-Location
  } else {
    Write-Host "no change - test number = [$($i)] $DateFormatSystem $Timer`n`n" -ForegroundColor Red
    } 

    # the next line will cause to pause for $time seconds here 
    # Remove this line as it will stop your waiting process from running up above.
    # Start-Sleep -Seconds $Time

}
1 Spice up

Hi,

it is not exaclty what I wanted but it works. The timer should not be like in a window or so.

It should be here at the end: < Write-Host "No change was made - loop number = [$($i)] Time for the next test (backup): (Here should be the time) " >

thanks for your help!

Hello again,

I did it but now I have another problem.

    $Time = 11
    for ($Timer = $Time; $Timer -gt 0; $Timer--) {
      Write-Host "`rNo change was made - loop number = ($($i)) - Next check in: $Timer" -NoNewLine -ForegroundColor Red
      Start-Sleep -Milliseconds 1000
  }
      Write-Host "`rNo change was made - loop number = ($($i)) - Next check in: $Timer" -ForegroundColor Red 
  } 

The problem is that it adds a zero after the number when it goes down.

Output:

11

10

90

80

70

How can I let it count down without the zero?

Thanks for help!

Add some spaces after $Timer" so like $Timer ". The new string is not overwriting the number from the previous string.

$Time = 11
    for ($Timer = $Time; $Timer -gt 0; $Timer--) {
      Write-Host "`rNo change was made - loop number = ($($i)) - Next check in: $Timer  " -NoNewLine -ForegroundColor Red
      Start-Sleep -Milliseconds 1000
  }
1 Spice up