Skip to content

Instantly share code, notes, and snippets.

@torgro
Created August 11, 2016 11:59
Show Gist options
  • Save torgro/c5685d37aa9f2b776aec2164d6c2f257 to your computer and use it in GitHub Desktop.
Save torgro/c5685d37aa9f2b776aec2164d6c2f257 to your computer and use it in GitHub Desktop.
function Get-SingleWeekDate
{
<#
.Synopsis
Get all dates for a specific day of the week on year forward. Please give me a better function name!!
.DESCRIPTION
Long description which is very short
.EXAMPLE
Get-SingleWeekDate -DayOfWeek Monday
Outputs all dates which is a Monday from the current date
.EXAMPLE
Get-SingleWeekDate -DayOfWeek 1
Outputs all dates which is a Monday with an integer input value from the current date.
The [System.WeekDay] enum translate 1 to Monday localized
.EXAMPLE
(Get-Date).AddYears(1) | Get-SingleWeekDate
Outputs all dates which is a Monday, starting one year from now and one year forward
.NOTES
@toregroneng tore.groneng@gmail.com
#>
[cmdletbinding()]
[OutputType([datetime[]])]
Param
(
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeLine)]
[System.DayOfWeek]
$DayOfWeek
,
[Parameter(ValueFromPipelineByPropertyName,ValueFromRemainingArguments)]
[Alias("Date")]
[datetime]
$StartDate = (Get-Date)
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
PROCESS
{
Write-Verbose -Message "$f - Finding the first occurance of the specified weekday [$DayOfWeek]"
foreach($counter in (0..6))
{
if(($StartDate).AddDays($counter).DayOfWeek -eq $DayOfWeek)
{
$firstAvailable = $StartDate.AddDays($counter)
break
}
}
Write-Verbose -Message "$f - Looping through and finding dates"
for ($i = $counter; $i -lt 365; ($i =$i+7))
{
$StartDate.AddDays($i)
}
}
END
{
Write-Verbose -Message "$f - END"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment