To convert a string to a date in PowerShell, cast it with [datetime] when it’s in US or ISO format. For any other layout, use [datetime]::ParseExact() and tell it the exact format:
$shipDate = [datetime]'2026-09-28'
$invoice = [datetime]::ParseExact('28.09.2026 14:30', 'dd.MM.yyyy HH:mm', [cultureinfo]::InvariantCulture)
"Ship date: $($shipDate.ToString('dddd, MMMM d, yyyy'))"
"Invoice: $($invoice.ToString('MMMM d, yyyy h:mm tt'))"
"Type: $($invoice.GetType().Name)"
Output:
Ship date: Monday, September 28, 2026
Invoice: September 28, 2026 2:30 PM
Type: DateTime
![PowerShell convert a string to a date with [datetime] and ParseExact](https://powershellfaqs.com/wp-content/uploads/2026/09/powershell-convert-string-to-date-parseexact.png)
Once it’s a DateTime, you can format it, compare it and do date math with it. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, and the results were identical.
[datetime] vs Get-Date: why the same string gives two dates
The [datetime] cast always reads month first, US style, no matter how the PC is set up. Get-Date follows the PC’s regional format. On a PC set to UK format, they disagree:
[System.Threading.Thread]::CurrentThread.CurrentCulture = 'en-GB' # a PC set to UK date format
$text = '04/05/2026'
"Cast [datetime]: $(([datetime]$text).ToString('MMMM d, yyyy'))"
"Get-Date: $((Get-Date -Date $text).ToString('MMMM d, yyyy'))"
Output:
Cast [datetime]: April 5, 2026
Get-Date: May 4, 2026
![PowerShell [datetime] cast vs Get-Date with a UK date format](https://powershellfaqs.com/wp-content/uploads/2026/09/powershell-datetime-cast-vs-get-date-culture.png)
If your script runs on computers with different regional settings, don’t rely on Get-Date to read a date string. Use the cast for US and ISO dates, or ParseExact for anything else.
Convert common US date formats with ParseExact
ParseExact needs a format string that matches the text exactly. [cultureinfo]::InvariantCulture makes month names and AM/PM read the same on every PC:
$us = [cultureinfo]::InvariantCulture
[datetime]::ParseExact('9/28/2026', 'M/d/yyyy', $us).ToString('yyyy-MM-dd')
[datetime]::ParseExact('Sep 28, 2026', 'MMM d, yyyy', $us).ToString('yyyy-MM-dd')
[datetime]::ParseExact('20260928', 'yyyyMMdd', $us).ToString('yyyy-MM-dd')
[datetime]::ParseExact('09/28/2026 3:45 PM', 'MM/dd/yyyy h:mm tt', $us).ToString('yyyy-MM-dd HH:mm')
Output:
2026-09-28
2026-09-28
2026-09-28
2026-09-28 15:45
| Text | Format string |
|---|---|
| 9/28/2026 | M/d/yyyy |
| 09/28/2026 | MM/dd/yyyy |
| Sep 28, 2026 | MMM d, yyyy |
| September 28, 2026 | MMMM d, yyyy |
| 09/28/2026 3:45 PM | MM/dd/yyyy h:mm tt |
| 20260928 | yyyyMMdd |
Capital MM is the month and lowercase mm is minutes, a common mix-up.
Check if a string is a valid date
ParseExact stops with an error when the text doesn’t match. For user input or file data, use TryParseExact. It returns True or False, and it accepts a list of formats:
$formats = [string[]]('M/d/yyyy', 'MM/dd/yyyy', 'yyyy-MM-dd')
$us = [cultureinfo]::InvariantCulture
foreach ($text in '9/28/2026', '2026-09-28', '28/09/2026') {
$date = [datetime]::MinValue
if ([datetime]::TryParseExact($text, $formats, $us, 'None', [ref]$date)) {
'{0,-11} -> {1:MMMM d, yyyy}' -f $text, $date
}
else {
'{0,-11} -> not a valid date' -f $text
}
}
Output:
9/28/2026 -> September 28, 2026
2026-09-28 -> September 28, 2026
28/09/2026 -> not a valid date
28/09/2026 has no 28th month, so it’s rejected instead of crashing the script.
How do I find a date inside a string?
TryParse only works when the whole string is a date. It won’t pull a date out of a sentence:
$line =