Converting a date into a string

PowerShell:
Converting a date into a string

How to:

To turn a date into a string, we use the ToString method or the -f format operator. Here’s how:

# Current date and time
$date = Get-Date

# Default conversion to string
$dateString = $date.ToString()
Write-Output $dateString

# Custom format: Year-Month-Day Hours:Minutes
$customFormat = $date.ToString("yyyy-MM-dd HH:mm")
Write-Output $customFormat

# Using -f operator for the same custom format
$fString = "{0:yyyy-MM-dd HH:mm}" -f $date
Write-Output $fString

Sample output:

2023-03-17 10:45:00
2023-03-17 10:45
2023-03-17 10:45

Deep Dive

PowerShell, inspired by Unix shells and Windows Script Host, introduced Get-Date in its early development around 2006. This became the go-to command for date-time operations. The ToString method on DateTime objects and the -f format operator are borrowed concepts from .NET, giving PowerShell its object-oriented flavors.

If ToString() isn’t specified with a format, it spits out the full date and time in the current culture’s format. But when you need a specific layout, like ISO 8601 or just the day and month, custom .NET date and time format strings become your pals.

There’s another old-school way—using DateTime’s format patterns like yyyy for a four-digit year, MM for zero-padded month. They are intuitive and plentiful for crafting any date-time format.

Then there’s POSIX in Unix, where date commands rule with their own format specifiers. PowerShell bridged the two worlds, adopting familiar methods but also providing heavy compatibility with Windows systems.

Alternatives include basic concatenation of date components and using external utilities or language infrastructures. PowerShell, however, prefers to keep things in-house with robust native commands.

You can dive deeper into format specifiers in the official Microsoft documentation or discover community-written blogs that often share creative ways to manipulate dates and times in PowerShell.

See Also