Skip to content

Instantly share code, notes, and snippets.

@jjbaumgartner
Last active August 28, 2018 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjbaumgartner/8888cd1a39413e4756e77ef88be493dc to your computer and use it in GitHub Desktop.
Save jjbaumgartner/8888cd1a39413e4756e77ef88be493dc to your computer and use it in GitHub Desktop.
Simple function to get the Nth ___day after a given date. Good for finding Patch Tuesday (2nd Tuesday of the month) or for scheduling your patches (pipelining, second Saturday after the second Tuesday of the month)
function Get-NthDay {
[CmdletBinding()]
Param(
$Nth = 2,
$Day = 'Tuesday',
[Parameter(ValueFromPipeline = $true)]$StartDate = (Get-Date "00:00:00")
)
# Calculate the next 7*$Nth days worth of dates...
# ...grab each one that is the weekday we're looking for...
# ...and then grab the Nth-1 (proper arrays start at 0) of those days. Poof, you have the Nth ___day of the month
(0..(7 * $Nth) | Foreach-Object { (Get-Date $StartDate).AddDays($_) } | Where-Object { $_.dayofweek -eq $Day })[$Nth - 1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment