I was working on some reporting in PowerShell this afternoon, and needed to figure out if two dates are in the same week.
Here’s a quick little function that I put together to get the week number given a specific date. If no [datetime] object is passed in, it will simply return the week number for the current date.

Screenshot of Get-WeekNumber in action
Here’s the code:
function Get-WeekNumber([datetime]$DateTime = (Get-Date)) { | |
$cultureInfo = [System.Globalization.CultureInfo]::CurrentCulture | |
$cultureInfo.Calendar.GetWeekOfYear($DateTime,$cultureInfo.DateTimeFormat.CalendarWeekRule,$cultureInfo.DateTimeFormat.FirstDayOfWeek) | |
} |