locked
How do I write a powershell script to select current business day in date? RRS feed

  • Question

  • I am currently working on writing a script to send a file to vendor on the first business day of the month. I have written the most part of the script but the part of the script where the date is checked if it's business day or not doesn't seem to work. I wrote the below script but it failed. Any help would be greatly appreciated.

    Function Get-currentbusinessDay {
    param($days)
    $date = get-date
    $newDate = $date.AddDays($days)
    switch ($newDate.DayOfWeek)
     
        {
           "Saturday" {$newDate = $newDate.AddDays(2); $newDate}
           "Sunday"   {$newDate = $newDate.AddDays(1); $newDate}
           Default {$newDate}
        }
    }

    if (get-currentbusinessday -days 0 -eq $date)

    • Moved by Just Karl Wednesday, March 9, 2016 2:51 PM Looking for the correct forum.
    Wednesday, March 9, 2016 3:26 AM

Answers

  • Hello,

    You could skip your function completely with this quickly cobbled together example:

    if ((Get-Date).DayOfWeek -notlike "s*"){
        Write-Host "WeekDay"
        #Do Stuff
    }
    

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    • Proposed as answer by Elaine Jing Friday, March 11, 2016 1:48 AM
    • Marked as answer by Elaine Jing Friday, March 25, 2016 3:37 PM
    Wednesday, March 9, 2016 2:50 PM
  • Hi,

    You could try with below code:

     Function Get-Currentbusinessday{
     param($days)
     $date= get-date
     $newdate= $date.AddDays($days)
     $M= ($newdate).DayOfWeek
     if($M -notlike "S*")
     {Write-Host "It's $newdate,$M"}
     else
     {Write-Host "It's weekend"}
     }

    I test above code it works on my lab.

    Hope it helps.

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Elaine Jing Friday, March 25, 2016 3:37 PM
    • Marked as answer by Elaine Jing Friday, March 25, 2016 3:37 PM
    Friday, March 11, 2016 3:11 AM

All replies

  • Hello,

    The TechNet Sandbox forum is designed for users to try out the new forums functionality. Please be respectful of others, and do not expect replies to questions asked here.

    As it's off-topic here, I am moving the question to the Windows PowerShell forum.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Wednesday, March 9, 2016 2:41 PM
  • Hello,

    You could skip your function completely with this quickly cobbled together example:

    if ((Get-Date).DayOfWeek -notlike "s*"){
        Write-Host "WeekDay"
        #Do Stuff
    }
    

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    • Proposed as answer by Elaine Jing Friday, March 11, 2016 1:48 AM
    • Marked as answer by Elaine Jing Friday, March 25, 2016 3:37 PM
    Wednesday, March 9, 2016 2:50 PM
  • Hi,

    You could try with below code:

     Function Get-Currentbusinessday{
     param($days)
     $date= get-date
     $newdate= $date.AddDays($days)
     $M= ($newdate).DayOfWeek
     if($M -notlike "S*")
     {Write-Host "It's $newdate,$M"}
     else
     {Write-Host "It's weekend"}
     }

    I test above code it works on my lab.

    Hope it helps.

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Elaine Jing Friday, March 25, 2016 3:37 PM
    • Marked as answer by Elaine Jing Friday, March 25, 2016 3:37 PM
    Friday, March 11, 2016 3:11 AM
  • Just keep it simple if you want to check if its a weekday

    $datenumber=[int](get-date).dayofweek
    if ("6","7" -notcontains $datenumber) {"Not a sunday or saterday"}

           

    Friday, March 11, 2016 12:34 PM