Ever wanted to know how many days you need to work this month? Here's a clever function that lists all the weekdays you want that are in a month. By default, Get-WeekDay returns all Mondays through Fridays in the current month. You can specify different months (and years), too, if you want to.
function Get-Weekday { param( $Month = $(Get-Date -format 'MM'), $Year = $(Get-Date -format 'yyyy'), $Days = 1..5 ) $MaxDays = [System.DateTime]::DaysInMonth($Year, $Month) 1..$MaxDays | ForEach-Object { Get-Date -day $_ -Month $Month -Year $Year | Where-Object { $Days -contains $_.DayOfWeek } } }
Pipe the result to Measure-Object to count the days.
PS> Get-Weekday | Measure-Object | Select-Object -ExpandProperty Count 22
You can even pick the weekdays you want to get included. The weekday ID starts with 0 (Sunday) and ends with 6 (Saturday). So this gets you all the weekends in the current month:
PS> Get-Weekday -Days 'Saturday', 'Sunday' | Measure-Object | Select-Object -ExpandProperty Count 9
And if you know each Monday is a soccer meeting, this will tell you how many Mondays there are this month:
PS> Get-Weekday -Days 'Monday' Monday, March 5, 2012 6:15:57 PM Monday, March 12, 2012 6:15:57 PM Monday, March 19, 2012 6:15:57 PM Monday, March 26, 2012 6:15:57 PM