PowerShell Get-Date To String

In this PowerShell tutorial, I will explain how to use Get-Date to convert dates into strings, making it easier to display. I will show a complete example of “PowerShell get-date to string“.

Convert Get-Date to String in PowerShell

The Get-Date cmdlet in PowerShell retrieves the current date and time from the system. It returns a DateTime object that can be formatted and manipulated in a variety of ways. By default, Get-Date displays the current system date and time in the long date and long-time formats.

There are times when you need to convert a DateTime object into a string, perhaps to format it in a specific way or to include it in a text file. The DateTime object has a ToString() method that allows you to specify the format of the date and time.

The ToString() Method

The ToString() method is a powerful way to convert dates and times to strings. You can use it without parameters to get the default string representation of the date and time, or you can pass a format string to get a customized representation.

Here’s the PowerShell code for this example:

# Get the current date and time
$currentDate = Get-Date

# Format the date as "DayOfWeek, Month Day, Year"
$friendlyFormattedDate = $currentDate.ToString("dddd, MMMM d, yyyy")

# Output the formatted date
Write-Host "Today is: $friendlyFormattedDate"

In the ToString() method, the custom format string "dddd, MMMM d, yyyy" is used. Here’s what each part of the format string represents:

  • dddd: The full name of the day of the week (e.g., Monday)
  • MMMM: The full name of the month (e.g., January)
  • d: The day of the month, without a leading zero for single-digit days (e.g., 4)
  • yyyy: The year in four digits (e.g., 2024)

When you run this script, you should see output similar to the following:

Today is: Monday, January 4, 2024

You can see the below screenshot for the output, I ran the PowerShell script using Visual Studio Code.

PowerShell Get-Date To String

Custom Date and Time Formats

PowerShell allows you to specify custom date and time formats using the .NET Framework format strings. For example, "d" represents the short date pattern, "D" stands for long date pattern, "t" is for short time pattern, and "T" is for long time pattern.

Here’s a simple example of converting the current date and time to a string using Get-Date and the ToString() method with a custom format:

$currentDate = Get-Date
$formattedDate = $currentDate.ToString("yyyy-MM-dd HH:mm:ss")
Write-Host "The formatted date and time is: $formattedDate"

In this example, yyyy-MM-dd HH:mm:ss is a custom format string that represents the year in four digits, the month in two digits, the day in two digits, followed by the hour, minute, and second in two digits each. The output will be something like:

The formatted date and time is: 2024-01-04 15:30:45

Conclusion

Converting Get-Date to a string in PowerShell is simple. With the ToString() method and custom format strings, you can convert a date to a string in PowerShell.

You may also like:

>