question

ChristopherJack-1763 avatar image
0 Votes"
ChristopherJack-1763 asked AndreasBaumgarten answered

Greater than a set time in Powershell

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered

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



image.png (18.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.