How to Convert Date to String in PowerShell (5 Simple Methods)

When working with PowerShell scripts, I frequently need to convert dates to strings for various purposes – whether it’s creating log files, generating reports, or naming files with timestamps. There are various methods to do in PowerShell.

In this tutorial, I will explain five different methods to convert dates to strings in PowerShell, using practical examples from my decade of experience.

Method 1 – Using the ToString() Method

The ToString() method is the most straightforward way to convert a date to a string in PowerShell. It’s incredibly versatile and allows you to specify the exact format you want.

Here’s how you can use it:

$currentDate = Get-Date
$dateString = $currentDate.ToString("MM/dd/yyyy")
Write-Output $dateString

This will output something like: 05/05/2025

You can see the exact output in the screenshot below:

powershell date to string

You can customize the format string to get exactly what you need:

# Different format examples
$currentDate = Get-Date

# Short date (MM/dd/yyyy)
$shortDate = $currentDate.ToString("MM/dd/yyyy")

# Long date with time (MM/dd/yyyy HH:mm:ss)
$longDate = $currentDate.ToString("MM/dd/yyyy HH:mm:ss")

# File naming friendly format (yyyy-MM-dd_HHmmss)
$fileNameDate = $currentDate.ToString("yyyy-MM-dd_HHmmss")

Write-Output "Short date: $shortDate"
Write-Output "Long date: $longDate"
Write-Output "Filename friendly: $fileNameDate"

Common Format Specifiers

Here are some format specifiers I use most frequently in PowerShell:

  • MM – Month (01-12)
  • dd – Day (01-31)
  • yyyy – Year as 4 digits
  • HH – Hour in 24-hour format (00-23)
  • mm – Minutes (00-59)
  • ss – Seconds (00-59)
  • tt – AM/PM designator

Check out PowerShell Get-Date Format

Method 2 – Using Get-Date with -Format Parameter

The Get-Date cmdlet has a built-in -Format parameter that makes date-to-string conversion even simpler. Here is an example.

$dateString = Get-Date -Format "MMM dd, yyyy"
Write-Output $dateString

This outputs something like: May 05, 2025

Here is the exact output in the screenshot below:

powershell convert date to string

I find this method particularly useful for creating report titles or headers:

$reportHeader = "Sales Report - $(Get-Date -Format 'MMMM yyyy')"
Write-Output $reportHeader

This gives you: Sales Report - May 2025

Check out PowerShell Get-Date Minus 1 Day

Method 3 – Using Custom Date and Time Formats

PowerShell allows you to use predefined format specifiers that represent standard date and time formats:

$date = Get-Date

# Short date pattern
$shortDate = $date.ToString("d")

# Long date pattern
$longDate = $date.ToString("D")

# Full date/time pattern (short time)
$fullDateShortTime = $date.ToString("f")

# Full date/time pattern (long time)
$fullDateLongTime = $date.ToString("F")

Write-Output "Short date: $shortDate"
Write-Output "Long date: $longDate"
Write-Output "Full date (short time): $fullDateShortTime"
Write-Output "Full date (long time): $fullDateLongTime"

This produces output like:

Short date: 5/5/2025
Long date: Friday, May 5, 2025
Full date (short time): Friday, May 5, 2025 9:30 AM
Full date (long time): Friday, May 5, 2025 9:30:45 AM

Standard Format Specifiers

These are some standard format specifiers I use often:

  • d – Short date pattern
  • D – Long date pattern
  • f – Full date/time pattern (short time)
  • F – Full date/time pattern (long time)
  • g – General date/time pattern (short time)
  • G – General date/time pattern (long time)

Read PowerShell Get-date Add Days Examples

Method 4 – Using the -UFormat Parameter

If you’re coming from a Unix/Linux background like me, you might prefer using the -UFormat parameter, which follows the Unix date formatting style:

$unixStyleDate = Get-Date -UFormat "%m/%d/%Y %H:%M:%S"
Write-Output $unixStyleDate

This will output: 05/05/2025 09:30:45

I often use this method when creating scripts that need to be consistent across different platforms:

# Log file naming with Unix-style format
$logFileName = "application_log_$(Get-Date -UFormat '%Y%m%d').log"
Write-Output "Log file created: $logFileName"

This creates a filename like: application_log_20250505.log

Common UFormat Specifiers

  • %m – Month (01-12)
  • %d – Day (01-31)
  • %Y – Year with century (2023)
  • %H – Hour in 24-hour format (00-23)
  • %M – Minutes (00-59)
  • %S – Seconds (00-59)
  • %A – Full weekday name (Sunday-Saturday)
  • %B – Full month name (January-December)

Read Compare Dates in PowerShell

Method 5 – Using the [datetime] Type Accelerator with Format-String

Sometimes I need to convert string dates to DateTime objects and then back to strings with different formatting. The [datetime] type accelerator is perfect for this:

# Converting a string date to DateTime and then to another format
$inputDateString = "2025-05-05T09:30:45"
$dateObject = [datetime]$inputDateString
$formattedDate = $dateObject.ToString("MMMM dd, yyyy")

Write-Output "Original: $inputDateString"
Write-Output "Converted: $formattedDate"

This outputs:

Original: 2025-05-05T09:30:45
Converted: May 05, 2025

This is particularly useful when processing dates from CSV files, APIs, or databases.

Real-World Example: Create a Daily Report Script

Here’s a practical example from a script I use to generate daily reports for a client in the healthcare sector:

# Generate daily patient admission report
$today = Get-Date
$yesterday = $today.AddDays(-1)

# Format dates for report
$reportDate = $today.ToString("MMMM d, yyyy")
$dataDate = $yesterday.ToString("yyyy-MM-dd")
$reportFileName = "Admissions_$(Get-Date -Format 'yyyyMMdd').csv"

# Create report header
$reportHeader = @"
Daily Patient Admission Report
Date: $reportDate
Reporting Period: $($yesterday.ToString("MMMM d, yyyy"))
Generated On: $($today.ToString("MM/dd/yyyy HH:mm:ss"))
"@

Write-Output $reportHeader
Write-Output "Report will be saved as: $reportFileName"

This script creates a professional report header with appropriate date formatting for different purposes – a human-readable date for the report title, a file-friendly format for the filename, and a detailed timestamp showing when the report was generated.

Check out Check If a Date Is Older Than 30 Days in PowerShell

Tips for Working with Date Strings in PowerShell

After years of creating scripts for various organizations, I’ve collected these useful tips:

  1. Be consistent with your formats: Choose a standard format for your organization and stick with it.
  2. Use ISO 8601 for technical purposes: For logs and machine-readable files, use the format yyyy-MM-ddTHH:mm:ss which is internationally recognized.
  3. Consider your audience: Use more readable formats like MMMM d, yyyy for reports that will be viewed by people.
  4. Be timezone-aware: When working with international systems, consider including timezone information:
   $dateWithTimeZone = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss zzz")
  1. Test with different regional settings: Your script might run on systems with different regional settings, so test accordingly.

In this tutorial, I explained how to convert a date to a string in PowerShell using various methods. PowerShell provides various options for this.

I hope you found this article helpful. If you have any questions or your own date formatting tips, please share them in the comments below!

You may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App