Hi Guys,
For script purpose I needed to have the information of the First and Last Day of the Week quickly and easily.
I already added the old, but great, solution provided by the Scripting Guy to get the week number out of the Get-ExtendedDate function using:
[ps]Get-Date -Date ’06/26/2017′ -UFormat %V[/ps]
So I took the liberty to add my own members in the existing extended Get-Date function to match my needs. I basicly used the DayOfWeek property to switch a Get-Date AddDays method. If it is the first day of the week, do nothing for the FirstDayOfWeek and add 6 days to get the LastDayOfWeek. If it is the second day of the week, add -1 day to find the FirstDayOfWeek and add 5 days to find the LastDayOfWeek and so on ….
It gave me something like this:
[ps]Function Get-ExtendedDate {
Param(
[DateTime]$Date = $(Get-Date)
)
$Output = Get-Date -Date $Date
#Number of the current Week in the year
Add-Member -MemberType NoteProperty -Name WeekOfYear -Value (Get-Date -Date $Output -UFormat %V) -inputobject $Output
#Date of the first and last day of the current week
switch ($Output.DayOfWeek.value__) {
‘1’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output -Hour 00 -Minute 00 -Second 00 -Millisecond 00)-InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(6) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘2’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-1) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(5) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘3’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-2) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(4) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘4’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-3) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(3) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘5’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-4) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(2) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘6’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-5) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(1) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘7’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-6) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
}
Write-Output -InputObject $Output
}[/ps]
Here is an example of the output and usage:
See you,