Greater than a set time in Powershell

Christopher Jack 1,611 Reputation points
2021-03-30T09:27:03.1+00:00

Hi,

I have a job that runs in Windows server task scheduler at 8am and 2pm.

I want my script to be able to say if the time is greater than 12pm then do something, else do something else.

Something similar to

$DateTimeNow = Get-Date 
if($DateTimeNow.TimeOfDay -gt 12:00pm){

} 
else {

}

What would be the best way to go about this?

TIA

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,361 Reputation points MVP
    2021-03-30T11:03:42.743+00:00

    Hi @ ChristopherJack-1763 ,

    this is working here:

    $DateTimeNow = (Get-Date).AddHours(-3) # the ".AddHours(-3)" is just for testing with different times, just remove this for your script  
    $DateTimeNow  
    if($DateTimeNow -gt "12:00 pm"){  
          Write-Output "later than 12:00 pm"   
    }   
    else {  
        Write-Output "before 12:00 pm"    
    }  
    

    82750-image.png

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful