How to: Add days to the current date: # Add 10 days to today's date $newDate = (Get-Date).AddDays(10) Write-Output $newDate Sample output: Thursday, April 13, 2023 Subtract days from the current date: # Subtract 15 days from today $pastDate = (Get-Date).AddDays(-15) Write-Output $pastDate Sample output: Wednesday, March 20, 2023 Calculate the difference between two dates: # Difference between two dates $date1 = Get-Date '2023-04-01' $date2 = Get-Date '2023-04-15' $diff = $date2 - $date1 Write-Output $diff.
Way back in the stone ages of computing—not really, but, you know, the early days—dates were messy.
To turn a date into a string, we use the ToString method or the -f format operator.
ToString
-f
PowerShell provides straightforward cmdlets for getting the date and time.
PowerShell makes parsing dates from strings straightforward with its Get-Date cmdlet and [datetime] type accelerator, which work well for standard date formats.
Get-Date
[datetime]