Greater than a set time in Powershell

Christopher Jack 1,601 Reputation points


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?


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.
4,731 questions
No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 67,531 Reputation points MVP

    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  
    if($DateTimeNow -gt "12:00 pm"){  
          Write-Output "later than 12:00 pm"   
    else {  
        Write-Output "before 12:00 pm"    



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

    Andreas Baumgarten

    1 person found this answer helpful.
    No comments

0 additional answers

Sort by: Most helpful