Exit script IF current date is December 24th or 25th regardless of future years

Welcome Forums General PowerShell Q&A Exit script IF current date is December 24th or 25th regardless of future years

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
11 months, 3 weeks ago.

  • Author
    Posts
  • #90014

    Participant
    Points: 0
    Rank: Member

    Hello!

    Exit .ps1 script IF current dates Decemeber 24th or December 25th regardless of future years. (Please, Leap year should be addressed)

    PowerShell version 2.0

    Your help will be really appreciated!

    Best Regards

  • #90019

    Participant
    Points: 1
    Rank: Member

    HI Sg68,
    not 100% sure what your looking for. But there are many ways to do achieve this. if your not concerned about the year try the code below

    $Date1 = "24/12"
    $Date2 ="25/12"
    
    $CurrentDate = (Get-Date -UFormat "%d/%m").ToString()
    
    if(($Date1 -eq $CurrentDate) -or ($Date2 -eq "$CurrentDate")){
        Write-Host "Current Date is $(Get-Date -UFormat "%d/%m/%Y")"
        Exit
    }
    else{
        Write-Host "Current date is not $($Date1) or $($Date2)"
    
    }
    

    Regards
    Shihan

  • #90020

    Participant
    Points: 332
    Helping Hand
    Rank: Contributor

    Try this...

    If((Get-Date -UFormat '%d/%m') -match '24/12|25/12')
    {Write-Warning -Message 'Current date is 24 or 25 December. Exiting process!';Exit}
    Else{'Current date is not 24 or 25 December. Process continuing'}

    • #90022

      Participant
      Points: 0
      Rank: Member

      Wow, Simple but smart!!!
      Thank you so much for your help! Will definitely try it.
      Regards

  • #90029
    Ron

    Participant
    Points: 0
    Rank: Member

    Get-Date returns an object with lots of information in it.

    > Get-Date | fl *
    
    DisplayHint : DateTime
    DateTime    : Tuesday, December 26, 2017 11:18:38 AM
    Date        : 12/26/2017 12:00:00 AM
    Day         : 26
    DayOfWeek   : Tuesday
    DayOfYear   : 360
    Hour        : 11
    Kind        : Local
    Millisecond : 109
    Minute      : 18
    Month       : 12
    Second      : 38
    Ticks       : 636498839181097017
    TimeOfDay   : 11:18:38.1097017
    Year        : 2017
    
    
    > $d=Get-Date
    > $d.Month
    > $d.Day
    
    12
    26
    • #90040

      Participant
      Points: 0
      Rank: Member

      Thank you for your reply!

The topic ‘Exit script IF current date is December 24th or 25th regardless of future years’ is closed to new replies.