powershell to calculate 20th business day from today - powershell

trying to calculate\determine 6th Business from today in powershell.
is there a script\function available for same?
example :
if today's date is 4th Feb 2018, 6th business day from today is 12th feb 2018.
how do i get that date using powershell?

I think this would work for your situation.
It adds 2 days if the current iteration lands on a Saturday and 1 day if it lands on a Sunday. There may be a better way but I know this works.
Edit: I didn't think about it if you need to account for holidays let me know and I may be able to edit this script to include it.
$daystoadd = 7
$CurrentDate = Get-date
For ($i = 1; $i -le $daystoadd; $i++) {
$CurrentDate = $CurrentDate.adddays(1)
if ($CurrentDate.DayOfWeek -eq 'Saturday') {
$CurrentDate = $CurrentDate.adddays(2)
}
if ($CurrentDate.DayOfWeek -eq 'Sunday') {
$CurrentDate = $CurrentDate.adddays(1)
}
}
$CurrentDate

#Sumanth specifically asks for the sixth business day, so this should do the trick:
function SixthBusinessDay($date)
{
$daysToAdd = 8,8,8,8,8,10,9
$weekDay = $date.DayOfWeek.value__
$sixtBusinessDay = $date.AddDays($daysToAdd[$weekDay])
return $sixtBusinessDay
}
It's based on that all days require at least six days and one weekend to make it the sixth business day, but Friday requires two extra days and Saturday one extra day.
Running this:
(SixthBusinessDay (Get-date "2018-1-29")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-1-30")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-1-31")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-1")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-2")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-3")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-4")).ToString("dddd dd/MM")
gives the following result:
Tuesday 06-02
Wednesday 07-02
Thursday 08-02
Friday 09-02
Monday 12-02
Monday 12-02
Monday 12-02

Related

Powershell how to check if friday is the last day of the month , returning True from a Function

I have tried to make a function in Powershell that produces a True if the last day of the month is friday.
function Get-LastFridayOfMonth([DateTime] $d) {
$lastDay = new-object DateTime($d.Year, $d.Month,[DateTime]::DaysInMonth($d.Year, $d.Month))
$diff = ([int] [DayOfWeek]::Friday) - ([int] $lastDay.DayOfWeek)
if ($diff -gT 0) {
return $lastDay.AddDays(- (7-$diff))
} else {
return $lastDay.AddDays($diff)
}
}
$testdate = Get-LastFridayOfMonth (Get-Date)
But how to check if that friday is the last day of the month ?
#Theo's answer reframes your question and gives a nice all-in-one solution, but to address your specific question:
"how to check if [a date] is the last day of the month ?"
you can just add one day to the date and see if the month changes. If it does change then the original date must be the last one in the month…
$testdate = [datetime] "2023-01-31"
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# True
$testdate = [datetime] "2023-01-30"
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# False
$testdate = Get-LastFridayOfMonth (Get-Date)
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# depends on if the month ends on a friday
I would change that function to just return the last days weekday so after calling it you can decide what weekday you get for the given date.
function Get-LastWeekDayOfMonth {
param (
[datetime]$Date = (Get-Date)
)
# .AddDays(-$Date.Day+1) sets the given date to the 1st of that month
$Date.AddDays(-$Date.Day+1).AddMonths(1).AddDays(-1).DayOfWeek
}
switch (Get-LastWeekDayOfMonth) {
'Friday' { "It's a Friday !" }
default { $_ }
}
Using the current date (February 2023) it would yield Tuesday
If you specify for instance March 2023
$d = Get-Date -Year 2023 -Month 3 -Day 1
switch (Get-LastWeekDayOfMonth -Date $d) {
'Friday' { "It's a Friday !" }
default { $_ }
}
The outcome will be It's a Friday !
If however you want a function to simply return $true or $false when the month end in a Friday or not, do this instead:
function Test-LastDayOfMonthIsFriday {
param (
[datetime]$Date = (Get-Date)
)
# .AddDays(-$Date.Day+1) sets the given date to the 1st of that month
$Date.AddDays(-$Date.Day+1).AddMonths(1).AddDays(-1).DayOfWeek -eq 'Friday'
}
Test-LastDayOfMonthIsFriday # --> $false
Test-LastDayOfMonthIsFriday -Date (Get-Date -Year 2023 -Month 3 -Day 1) # --> $true
Special thanks to mklement0 for the $Date.AddDays(-$Date.Day+1) part which always sets the date to the first day of that month

How can I check if today is the first Tuesday of the current quarter?

I'm trying to find in Powershell if today's date is greater than the first Tuesday of the quarter or if it is before the first Tuesday of the quarter.
The reason is my script should only make a change if today is past the first Tuesday of the quarter!
I know how to do it for the first Tuesday of a month, but really struggling to get it to work with quarters, whats the best way to do this?
# Get today's date (without a time-of-day component).
$today = (Get-Date).Date
$afterFirstTueInQuarter =
if ($today.Day -ge 7 -or $today.Month -notin 1, 4, 7, 10) { $true }
else { # in the first 6 days of the start of a quarter
# Find the first day of the month...
$firstDayOfMonth = Get-Date -Year $today.Year -Month $today.Month -Day 1
# ... and the month's first Tuesday.
$firstTue = $firstDayOfMonth.AddDays(([DayOfWeek] 'Tuesday' - $firstDayOfMonth.DayOfWeek + 7) % 7)
# Compare to today.
$today -gt $firstTue
}

Using powershell for a google report

I'm new to using powershell, I'm trying to use the PSGSuite module to get a report for all users for the past 30 days. What I've got so far is the following
$REQUESTEDDATE = Read-Host -Prompt 'Enter the month for the desired report month in numerical form. e.g Jan 1 Feb 2 Mar 3'
$REPORTDATE = (Get-Date -Month ($REQUESTEDDATE-(-1)) -Hour 0 -Minute 0 -Second 0)
$MonthAgo = $REPORTDATE.AddMonths(-1)
$FIRSTDAYOFMONTH=GET-DATE $MonthAgo -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)
$Times = $FIRSTDAYOFMONTH..$LASTDAYOFMONTH.day | Foreach-Object {
$currentdate = Get-Date -Day $_ -Month $LASTDAYOFMONTH.Month -Year $LASTDAYOFMONTH.Year
$GMAIL = Get-GSUsageReport -Date $currentdate -UserKey xxx -flat
}
This is now throwing a "Invalid cast from 'DateTime' to 'Int32' error. There's probably a much easier way to do this, but I'm more of the hardware/networking side thrown onto this while the dev team is working on different projects, so any help is appreciated.
First thing is that I hate all those CAPITALS in your code, so if you don't mind I have changed that.
Next, you can simply run through the dates for as long as the running date is less than the final date (day 1 of the requested date) using a while loop:
$requestedDate = Read-Host -Prompt 'Enter the month for the desired report month in numerical form. e.g Jan 1 Feb 2 Mar 3'
$reportDate = (Get-Date -Month $requestedDate -Day 1).Date # end date set at midnight on day 1
$runningDate = $reportDate.AddMonths(-1) # start at day 1, one month ago
$result = while ($runningDate -lt $reportDate) {
# perform your command and capture the output in variable $result
Get-GSUsageReport -Date $runningDate -UserKey xxx -flat
# increment the running date
$runningDate = $runningDate.AddDays(1)
}
# show the result
$result

Week Number with Get-Date

I have this powershell command:
Get-Date -UFormat %V
On Monday it shows bad week number and I don't know why. How do I add one to get the correct week number? I set everywhere that first day of week is Monday but didn't help. Thanks.
Try using this method to get the week number of the year. Found here
function Get-WeekNumber([datetime]$DateTime = (Get-Date)) {
$cultureInfo = [System.Globalization.CultureInfo]::CurrentCulture
$cultureInfo.Calendar.GetWeekOfYear($DateTime,$cultureInfo.DateTimeFormat.CalendarWeekRule,$cultureInfo.DateTimeFormat.FirstDayOfWeek)
}
Get-WeekNumber # -> 7, for instance
What you are after is the ISO8601 weeknumber, where
a week starts at Monday
week 1 is the first week that contains a Thursday
To get this weeknumber, you can use the function below:
function Get-Iso8601WeekNumber([datetime]$DateTime = (Get-Date)) {
# ISO8601: Week starts on Monday and Week 1 is the week that contains Thursday
$calendar = [CultureInfo]::InvariantCulture.Calendar
$dow = $calendar.GetDayOfWeek($DateTime)
if ($dow -ge [DayOfWeek]::Monday -and $dow -le [DayOfWeek]::Wednesday) {
$DateTime = $DateTime.AddDays(3)
}
return $calendar.GetWeekOfYear($DateTime, [Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
}
Using your example
Get-Iso8601WeekNumber "2020-02-17"
returns 8
Note the use of InvariantCulture as opposed to CurrentCulture

PowerShell returns 2 as week number on January 06 2020

A quick question, apparently today (January 06, 2020) week number should be 2, because there are 53 weeks in 2020.
However, the following PowerShell snippet returns 1:
(Get-Date -UFormat %V)
What is the good approach getting the week number properly?
To translate this Get the correct week number of a given date C# answer from #il_guru into PowerShell:
Function GetIso8601WeekOfYear([DateTime]$Date) {
$Day = (Get-Culture).Calendar.GetDayOfWeek($Date)
if ($Day -ge [DayOfWeek]::Monday -and $Day -le [DayOfWeek]::Wednesday) {$Date = $Date.AddDays(3)}
(Get-Culture).Calendar.GetWeekOfYear($Date, 'FirstFourDayWeek', 'Monday')
}
GetIso8601WeekOfYear (Get-Date)
2
GetIso8601WeekOfYear (Get-Date('2016-01-01'))
53
You could detect a leap year and then adjust the week number based off the result.
if(((Get-Date).year)%4 -eq 0){
$week = (Get-Date -UFormat %V) -as [int]
$week++
}else{
$week = (Get-Date -UFormat %V)
}
Write-Host $week