I'm confused. I don't see how "$_.DayOfWeek -gt 0" is working because on my pc (Win11, PS5.1) that is an alpha value.
DateTime : Wednesday, May 25, 2022 12:00:00 AM
Date : 5/25/2022 12:00:00 AM
Day : 25
DayOfWeek : Wednesday
DayOfYear : 145
Hour : 0
Kind : Unspecified
Millisecond : 0
Minute : 0
Month : 5
Second : 0
Ticks : 637890336000000000
TimeOfDay : 00:00:00
Year : 2022
As to the question; 10 business days is the known value and the problem is to solve for the calendar date. Correct?
Here is what I came up with.
$days = 10 # number of past business days to calculate
$weekend = 'Saturday','Sunday'
$startdate = Get-Date
$busdays = @() # array of business days
while($true) {
$startdate = $startdate.AddDays(-1)
if ($weekend -notcontains $startdate.DayOfWeek) {
$busdays += $startdate
}
if ($busdays.count -ge $days) {
break
}
}
cls
"Here are the prior $days business days."
$busdays
""
"Here is the date you are looking for."
$busdays[$days - 1] # because the array in relative to zero