Hoping this won't be too difficult but I am writing a script that queries user information from the domain and exports to CSV.
The script is run by our administrative staff and all they need to input into the script is the username.
The tricky part is that I need the CSV to be named based on the coming Friday.
Note: I am in Australia so my date format is DD-MM-YYYY
The way I am currently looking at going about it is as below:
# Grab the Script Run Timestamp
$ScriptRuntime = Get-Date -Day 4 -Month 5 -Year 2013
# Grab the next Friday (including today)
$NextFriday = $ScriptRuntime.AddDays(0 + $(0,1,2,3,4,5,6 -eq 5 - [int]$ScriptRuntime.dayofweek))
Write-Host "Script Run: "$ScriptRuntime
Write-Host "Next Friday: "$NextFriday
This works OK with all days except Saturday.
What am i doing wrong?
This finds the next Friday:
$date = Get-Date
while ($Date.DayOfWeek -ne "Friday") {$date = $date.AddDays(1)}
I'm not sure I'm following you but here's I would get next Friday:
$date = Get-Date
for($i=1; $i -le 7; $i++)
{
if($date.AddDays($i).DayOfWeek -eq 'Friday')
{
$date.AddDays($i)
break
}
}
Here's a one-liner that'll do the trick too:
$Date = @(@(0..7) | % {$(Get-Date).AddDays($_)} | ? {$_.DayOfWeek -ieq "Friday"})[0]
And if you need a specific time:
$Date = @(@(0..7) | % {$(Get-Date "12:00").AddDays($_)} | ? {($_ -gt $(Get-Date)) -and ($_.DayOfWeek -ieq "Friday")})[0]
A bit late to the party, but here's my take (no loops, no conditionals):
[datetime] $MyDate = get-date
[DayOfWeek] $NextDayOfWeek = 'Friday'
$MyDate.AddDays( (7 - [int] $MyDate.DayOfWeek + [int] $NextDayOfWeek )%7 )
If you want to verify it for a larger number of dates:
[datetime] $MyDate = get-date
[DayOfWeek] $NextDayOfWeek = 'Friday'
1..30 | ForEach-Object {
[PSCustomObject]@{
DayOfWeek = $MyDate.DayOfWeek ;
Date = $MyDate;
$("Next$NextDayOfWeek") = $MyDate.AddDays((7 - [int] $MyDate.DayOfWeek + [int] $NextDayOfWeek )%7)
};
$MyDate = $MyDate.AddDays(1)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With