Lee_Dailey

function Get-FirstMondayOfMonth

Mar 29th, 2022 (edited)
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-FirstMondayOfMonth
  2.     {
  3.     <#
  4.     comment based help goes here
  5.     #>
  6.  
  7.     [CmdletBinding ()]
  8.     Param (
  9.         [Parameter(
  10.             Position = 0
  11.             )]
  12.             [ValidateRange (1,12)]
  13.             [int]
  14.             $MonthNumber = (Get-Date).Month
  15.         )
  16.  
  17.     if ($MonthNumber)
  18.         {
  19.         $Day_1 = Get-Date -Month $MonthNumber -Day 1
  20.         }
  21.         else
  22.         {
  23.         $Day_1 = Get-Date -Day 1
  24.         }
  25.  
  26.     switch ($Day_1.DayOfWeek.value__)
  27.         {
  28.         0 {
  29.             $Offset = 1
  30.             break
  31.             }
  32.         1 {
  33.             $Offset = 0
  34.             break
  35.             }
  36.         default {
  37.             # the "+ 1" is to handle the "off-by-one" array index difference
  38.             $Offset = 7 - $Day_1.Date.DayOfWeek.value__ + 1
  39.             }
  40.  
  41.         }
  42.  
  43.     $Day_1.Date.AddDays($Offset)
  44.  
  45.     }
Add Comment
Please, Sign In to add comment