This April month is full of surprises! Not only I did not have the opportunity to trick my colleagues on Sunday April first, but I had some bug of my code due to the start of the week.
[int](get-date).dayofweek and (Get-WmiObject Win32_LocalTime).dayofweek both refer Sunday as day 0.
Wednesday, 11. April 2012 18:31:52
PS C:\svn> [int](get-date).dayofweek
PS C:\svn> (Get-WmiObject Win32_LocalTime).dayofweek
Consistently and unfortunately for me, the week does start on Sunday. And not on Monday.
To get week ending on Sunday, lets add 6 for Monday (5 for Tuesday, and so on) to dayofweek and get the 7-modulo
Ok, now Mon=0, Tue=1, Wed=2, etc…
Substract it from day of month to truncate to Monday
PS> (get-date).day - (6+(get-date).dayofweek)%7
First day of current week is Monday 9th
Now add 5 to get the first Day of month between 0 and 6
Divide by 7…
add 1 to get first week=1 (and not 0).
PS> [math]::floor(((get-date).day - (6+(get-date).dayofweek)%7 + 5)/7)+1
Which slicely differs from weekinmonth when 1st of month is Sunday !
PS> (Get-WmiObject Win32_LocalTime).weekinmonth