Back to Basics: Use PowerShell to Generate a List of Week Commencing Dates

The other day I need a year's worth of 'week commencing' dates to complete a work-related task. The thought of collecting them manually never entered my head!

Here's what I did:

$Start = Get-Date 27/06/2016

$i = 1

Do {

Add-Content -Value "$($Start.Day)/$($Start.Month)/$($Start.Year)" -Path dates.txt

$Start = $Start.AddDays(7)

$i++

}

Until ($i -eq 52)

 

Simple enough:

  • Create a datetime object with the first week commencing date
  • Set a counter to 1
  • Use a Do Until loop to...
    • write the current iteration's week commencing date to a file
    • increment the datetime object by 7 days
    • increment the count by
  • Keep looping until 52 entries have been added to the file