Finding Working Days

by Jul 2, 2014

To find all working days in a given month, here is a neat little one-liner:

$month = 7
1..31 | ForEach-Object { Get-Date -Day $_ -Month $month } | 
  Where-Object { $_.DayOfWeek -gt 0 -and $_.DayOfWeek -lt 6 }

Simply assign the month to $month (the example examines the month of July).

With a couple of more commands, the pipeline returns the number of working days as a number, too:

$month = 7
1..31 | ForEach-Object { Get-Date -Day $_ -Month $month } | 
  Where-Object { $_.DayOfWeek -gt 0 -and $_.DayOfWeek -lt 6 } | 
  Measure-Object |
  Select-Object -ExpandProperty Count

Twitter This Tip! ReTweet this Tip!