How to use the .AddMonths method of Get-Date in Powershell - powershell

I am writing a simple script that gets the date, gets the date 6 months from now, and copies both of those dates to the clipboard. When run, the script is supposed to copy:
Terminated MM/dd/yy - Delete from AD on MM/dd/yyyy
But it only copies
Terminated MM/dd/yyyy - Delete from AD on
$currentDate = Get-Date -Format "MM/dd/yyyy"
$futureDate = Get-Date.AddMonths(6) -Format "MM/dd/yyyy"
$copyThisText = "Terminated " + $currentDate + " - Delete from AD on " + $futureDate
$copyThisText | clip

The reason it failed is because once you format a date using -Format "MM/dd/yyyy" it converts that variable to a type of string rather than datetime which then means that the normal datetime methods are no longer available.
For demonstration purposes I have tried to change as little as possible. What I have done below is set the $currentDate and $futureDate without implicitly converting them to strings. I then format them the way you want when you are concatenating the strings in $copyThisText.
This will do what you want.
$currentDate = Get-Date
$futureDate = (Get-Date).AddMonths(6)
$copyThisText = "Terminated " + $currentDate.tostring("MM/dd/yyyy") + " - Delete from AD on " + $futureDate.tostring("MM/dd/yyyy")
$copyThisText | clip
Furthermore there are multiple ways to format the strings that could aid in readability of the code. Thanks #Santiago Squarzon for the suggestion -
"Terminated {0:MM/dd/yyyy} - Delete from AD on {1:MM/dd/yyyy}" -f $currentDate, $futureDate

Related

Powershell - Find the latest Friday

How can the following code be modified to identify the latest Friday within the past week (instead of the next one), but with formatting?
$Date = #(#(0..7) | % {$(Get-Date).AddDays($_)} | ? {$_.DayOfWeek -ieq "Friday"})[0]
Source: https://stackoverflow.com/a/23939203/5651418
The post you linked to offers a more elegant solution, which you can adapt as follows:
# Get the most recent Friday relative to the given date,
# which may be that date itself.
$mostRecentFriday =
($date = Get-Date).AddDays((-7 - $date.DayOfWeek + [DayOfWeek]::Friday) % 7)
If you want to create a formatted string representation of the resulting [datetime] instance (all examples below yield something like '07 01 2022':
To use Unix-style format specifiers, use Get-Date's -UFormat parameter:
Get-Date $mostRecentFriday -UFormat '%d %m %Y'
To use .NET's format specifiers, use Get-Data's -Format parameter:
Get-Date $mostRecentFriday -Format 'dd MM yyyy'
Alternatively, pass the format string to the [datetime]
instance's .ToString() method:
$mostRecentFriday.ToString('dd MM yyyy')
If I understood correctly, your expected output would be 1 7 2022, I would personally use a do loop that stops as soon as the DayOfWeek Property of the DateTime instance is Friday:
$date = [datetime]::Now
do {
$date = $date.AddDays(-1)
} until($date.DayOfWeek -eq [DayOfWeek]::Friday)
$date.ToString("d M yyyy")
I noticed that some Get-Date -UFormat specifiers didn't seem to work when attempting to incorporate them into an output string.
Should anyone need to incorporate some rarely needed ones (like 'Week of Year' (%G), 'Day of Year (%j), etc) you could preset needed variables and add them to the output string:
$DayOfYear = (Get-Date -UFormat %j)
$WeekOfYear = (Get-Date -UFormat %V)
$Date = #(#(0..7) | % {$(Get-Date).AddDays(-$_)} | ? {$_.DayOfWeek -ieq "Wednesday"})[0].ToString("MM-dd-yyyy|Week $WeekOfYear|'Day' $DayOfYear")
I imagine someone could incorporate all the code into one Powershell command.
Additional Get-Date -UFormat specifiers: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.2#notes

Powershell keep looping until condition is true then proceed

I have written a script that so far is able to check a file "latest.json" for the "created_at" object which shows the last date that a commit has occurred for software.
$websiteJson = Invoke-WebRequest "https://website/latest.json" | ConvertFrom-Json | select created_at
$todaysDate = Get-Date -Format "yyyy-MM-dd HH:mm"
if($websitejson.created_at | where {$_.created_at -eq $todaysDate}){
Write-Output "Today's date matches"
} else {
Write-Output "has not yet been updated"
}
How part of latest.json looks like
"created_at":"2020-03-23 17:32:48"
How do I change this to keep looping until the date pull from latest.json matches then proceed to next step (would download and install software). Also, since "created at" has "17:32:48" will this cause the date check to fail since the time does not match?
. I want it to keep checking if dates match.
Thank you!
Right now, I'm not going to bother converting dates to match to make sure they're the same format, but what you need for your specific questions is just a do until loop. I might update this to check the date formats if you supply an example layout of the returned JSON.
Do{
$websiteJson = Invoke-WebRequest "https://website/latest.json" | ConvertFrom-Json | select created_at
$todaysDate = Get-Date -Format "yyyy-MM-dd HH:mm"
if($websitejson.created_at | where {$_.created_at -eq $todaysDate}){
Write-Output "Today's date matches"
} else {
Write-Output "has not yet been updated"
}
start-sleep -s 60
}until($websiteJson -eq $todaysDate)
I believe this wont work right off the bat. You'll have to get the JSON date and $todaysDate to be the same format, then you can do this and it will work.
if you want to compare the date and/or time, use datetime objects instead of datetime strings. something like this ...
if you want to test for the actual time difference between two time objects ...
((Get-Date -Date '2020-03-23 18:11:22') - [datetime]'2020-03-23 17:32:48').TotalHours
# result = 0.642777777777778
you keep mentioning date as if you don't want the time, so this method would work for comparing the date parts of two timestamps ...
# the date at the time the code was run = 2020 April 03, Friday 4:30:34 PM
$Today = (Get-Date).Date
$Created_At = '2020-04-03 15:15:15'
$Today -eq ([datetime]$Created_At).Date
result = True

Convert date and time string into datetime

I've got a variable of type string which looks something like this
$string = "07/07/2019 18:00". I want to convert this variable into an variable of type datetime. The format should be MM/DD/YYYY HH:MM
$date = '07/07/2019'
$time = '18:00'
$datetime = $date + ' ' + $time
$datetime = [datetime]::ParseExact('$datetime', 'MM/DD/YYYY_HH:MM', $null)
Using the code above, I get an error message telling me:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
Is there another way to go?
Your format string used wrong specifiers, namely DD and YYYY; see custom date and time formats.
Change your code to
$date = '07/07/2019'
$time = '18:00'
$datetime = $date + ' ' + $time
$datetime = [datetime]::ParseExact($datetime, 'MM/dd/yyyy HH:mm', $null)
$datetime
Also be aware to pass $datetime as reference and not as single quoted string.
You find above code under this link.
[datetime]'07/07/2019 18:00'
Sunday, July 7, 2019 6:00:00 PM
Or
[datetime]'7/7'
[datetime]'18:00'
[datetime]'6pm'
Then you can add or subtract them, but then the answer is a [timespan].

PowerShell - formatting date to yyMMdd format

I run a PowerShell script that passes a parameter, such as "20160428".
I need to format this into yyMMdd (i.e., getting "160428" as the result). I could just strip out the "20", but I would like to get this right. For such I did so many attempts, such as:
#$COBDATE= '{0:yyMMdd}' -f $COBDATE
#$COBDATE = ([datetime]::ParseExact($COBDATE, "yyMMdd", [System.Globalization.CultureInfo]::InvariantCulture )).DayOfWeek
And the last one:
$COBDATE = ("{0:yyMMdd}" -f [datetime]::parse($COBDATE))
The below works, but once I replace "Get-Date" by my date "20160428" it just prints out the yyMMdd string.
$b = (Get-Date).AddDays(-1).ToString("yyMMdd")
So if I try this:
$input = "20160428"
$format = "yyMMdd"
$input_toDate_up = [DateTime]::ParseExact($input, $format, $null).ToString($format)
$input_toDate_up
It just says that the string is not a valid Date Time, which seems to be the root cause.
How can I fix this?
$Input = "20160428"
Get-Date -Year $Input.Substring(0,4) -Month $Input.Substring(4,2) -Day $Input.Substring(6,2) -Format "yyMMdd"
I think for one $input is a reserved variable so you shouldn't use it as nothing will really work in there as you expect. See here about_Automatic_Variables
I have used this drawn out substring process before with varied abstract date formats.
$dateinput = "20160428"
$dateformat = "yyMMdd"
Get-Date($dateinput.Substring(4,2) + "/" + $dateinput.Substring(2,2) + "/" + $dateinput.substring(0,4)) -Format $dateformat
I'm sure there is a shorter regex method, but that is not under my hat yet.

Special characters in PowerShell

I am trying to work with PowerShell and have it output the copyright character into a Microsoft Word document. For example, the code listed below is what I am trying to use and it is not working.
$SummaryPara.Range.Text = "© "
$SummaryPara.Range.Text = "Get-Date -Format yyyy"
$SummaryPara.Range.Text = " - Name of Org Here "
$SummaryPara.Range.InsertParagraphAfter()
Do I need to use the Alt + 0169 sequence somehow?
I am not sure what I am doing wrong since the following code seems to work:
$selection.TypeParagraph()
$selection.TypeText("© ")
$selection.TypeText((Get-Date -Format yyyy))
$selection.TypeText(" - Name of Org Here ")
$selection.TypeParagraph()
How can I make this work for both the copyright character and other similar special characters?
There are a few problems here. I'll list those and address each:
You can get any character you need by casting the Unicode representation to a char. In this case
[char]0x00A9
You assign a new value to $SummaryPara.Range.Text three times. So, you are overwriting the previous value each time, instead of concatenating ('+' operator), which I think is what you were trying to do.
You are trying to use the cmdlet Get-Date, but since you have quoted it, you will end up with the literal string "Get-Date -Format yyyy", instead of the result of the cmdlet.
Putting it all together, I think you want something like this:
$word = New-Object -ComObject Word.Application
$doc = $word.Documents.Add()
$SummaryPara = $doc.Content.Paragraphs.Add()
$SummaryPara.Range.Text = [char]0x00A9 + ($date = Get-Date -Format yyyy) + " - Name of Org Here "
$word.Visible = $true