sizz sizz - 3 months ago 25
PowerShell Question

While loop continuously looping comparing times

$time = Get-Date -DisplayHint Time
$addTime = Read-Host -Prompt "How many minutes until computer turns off?"

$addTimeConvert = [timespan]::FromMinutes($addTime)
$addTimeConvertOut = $addTimeConvert.ToString("hh\:mm\:ss")

$newTime = $time + $addTimeConvert


Write-Host $newTime
Write-Host $time

while ($newTime -ne $time){

$time = Get-Date -DisplayHint Time



}
Stop-Computer


I want to write a script to turn off my computer after a set amount of time but for some reason the while loop continues even after they are the same time.

Answer

Rather than answer your asked question, can I offer an alternative solution to the (probable) original problem?

$minutes = Read-Host -Prompt "How many minutes until computer turns off?"
Start-Sleep -Seconds (60 * $minutes)
Stop-Computer