The Get-Date cmdlet is used to retrieve the current date and time. We can use this cmdlet to get the date and time in different formats, perform date comparisons, and perform common date-time operations in PowerShell.
If the cmdlet is run without parameters, it returns the current date and time:
Get-Date
Display all properties of a DateTime object:
Get-Date | Select *
In this way, you can retrieve the value of any property of the DateTime object:
(Get-Date).year
You can extract only the time or only the date:
Get-Date -DisplayHint Time
Get-Date -DisplayHint Date
You can use the built-in methods of the Get-Date cmdlet to extract specific components of a date, such as the day of the week, the hour, the month, and so on. For example:
(Get-Date).DayOfWeek
(Get-Date).DayOfYear
(Get-Date).Hour
(Get-Date).Month
Or convert the date to UTC format:
(Get-Date).ToUniversalTime()
Hint. List all available cmdlet methods:
Get-Date | Get-Member
Important. When you use the -Format parameter, Get-Date returns a string instead of a DateTime object. If you need to perform additional date calculations, you should avoid formatting the value until the final output stage.
Customizing the output using the -Format parameter
The output date format can be customized using the –Format parameter. The following default date and time formats are available:
Parameter
Description
d
ShortDatePattern
D
LongDatePattern
f
long date and short time
F
long date and long time
g
General (short date and short time)
G
General (short date and long time)
m, M
MonthDayPattern
o
Round-trip date/time pattern
r, R
RFC1123Pattern
s
SortableDateTimePattern (based on ISO 8601)
t
ShortTimePattern
T
LongTimePattern
u
UniversalSortableDateTimePattern
U
long date and long time UTC format
y, Y
YearMonthPattern
For example, to display general short date and short time:
Get-Date -Format G
You can customize the separator characters in the datetime format as needed:
Additionally, you can customize the returned date and time format using custom specifiers:
yyyy, yy
Formats for year
M, %M, MM, MMM, MMMM
Month formats
d, %d, dd, ddd, dddd
Day formats
h, %h, hh, H, %H, HH
Hours
m, %m, mm
Minutes
s, %s, ss
Seconds
t, %t, tt
AM/PM switch
z, %z, zz, zzzz, K
Time zone
By using specifiers, you can customize the date format to suit your needs:
Get-Date -Format "HH:mm:ss dd/MM/yyyy"
Most common time format representations:
HH:mm
24-hour time format
HH:mm:ss
24-hour time format with seconds
hh:mm
12-hour format
hh:mm tt
12-hour format with AM/PM
K
UTC time zone offset
To output datetime in a custom Unix format, use the -UFormat option:
Get-Date -UFormat "%Y, %m, %d, %A %r %t"
Convert a String to DateTime
In many PowerShell scripts, dates are read from CSV files, log files, JSON data, or user input. Before you can compare dates or perform calculations, you may need to convert a string value into a DateTime object.
Here is an example:
Get-Date "2025-12-31"
You can also use the .NET DateTime type accelerator:
[datetime]"2025-12-31"
You can verify that the result is a DateTime object:
([datetime]"2025-12-31").GetType()
Once converted, you can use any DateTime method:
([datetime]"2025-12-31").AddDays(10)
Note. Date parsing may depend on the regional settings of the machine. In order to avoid ambiguity, you should use ISO 8601 date formats (such as yyyy-MM-dd) whenever possible.
When processing dates from files/external systems, ParseExact provides more predictable results because it doesn’t rely on the current regional settings:
One of the most common uses of the Get-Date cmdlet is generating unique file names for logs, reports, and backups. For example, you can append the current timestamp to a log file name:
This format is commonly used because it is sortable and safe for Windows file names.
Important. The Get-Date cmdlet returns a System.DateTime object. By default, DateTime values represent local/UTC time, but they do not preserve the original time zone offset when serialized/exchanged between systems. If you need to work with multiple time zones, APIs, cloud services, or Unix timestamps, it’s better to consider using System.DateTimeOffset instead. Here is an example:
(Get-Date).GetType()
(Get-Date) | Select DateTime, Kind
[DateTimeOffset]::Now
You can use the same method when creating backup folders/exporting reports:
Tip. In order to avoid using characters such as : or / in file names because they are not allowed in Windows paths. Formats like yyyyMMdd-HHmmss are commonly used for log files, backups, and scheduled task outputs.
Converting DateTime to Unix Timestamp
Some APIs, Linux systems, cloud services, and JSON-based apps use Unix timestamps instead of standard DateTime values. A Unix timestamp represents the number of seconds that have elapsed since January 1, 1970 (UTC).
To get the current Unix timestamp in seconds, use the command:
[DateTimeOffset]::Now.ToUnixTimeSeconds()
To return the Unix timestamp in milliseconds, run the following command:
[DateTimeOffset]::Now.ToUnixTimeMilliseconds()
You can also convert a specific date to a Unix timestamp with the command below:
Tip. Unix timestamps are always based on UTC time. When working with APIs or systems in different time zones, make sure you understand whether the returned value represents UTC/local time.
Keep in mind that the -UnixTimeSeconds parameter is available in modern PowerShell versions. For maximum compatibility across different PowerShell versions, you can use the DateTimeOffset methods shown above.
Get-Date -UnixTimeSeconds 1767139200
Using methods like AddDays, AddYears, AddHours
Using methods like AddDays, AddYears, AddHours, etc., you can add or subtract a specific amount of time from the current date.
For example, to get the date that was 10 days ago:
(Get-Date).AddDays(-10)
Or to find the date that will be in 5 years:
(Get-Date).AddYears(5)
The Get-Date cmdlet is commonly used to calculate the time difference (timespan) between two dates.
For example, the following command displays the number of days remaining until the next New Year:
The output of the command mentioned above may vary depending on the selected culture. For example, the same date can be displayed differently in US and German regional formats:
$CurrentDate = Get-Date
$USCulture = [CultureInfo]'en-US'
$GermanCulture = [CultureInfo]'de-DE'
$CurrentDate.ToString('D', $USCulture)
$CurrentDate.ToString('D', $GermanCulture)
Tip. You can check the current PowerShell culture settings using the built-in automatic variables:
$PSCulture
$PSUICulture
`$PSCulture` determines how dates, numbers, and other regional formats are interpreted and displayed. `$PSUICulture` specifies the language used for the PowerShell and Windows user interface. These values may differ between systems and can affect date parsing and formatting behavior in scripts.
Note. By default, date formatting depends on the current Windows regional settings. When generating reports, exporting data, or exchanging info with other systems, you should explicitly specifying a culture (this will help you to ensure consistent formatting).
Note that by default, Get-Date formatting depends on the current Windows regional settings. The same command may return different results on systems configured for en-US, en-GB, de-DE, or other locales.
Common Get-Date Examples
Here are some practical examples of using the Get-Date cmdlet in everyday PowerShell scripts.
To get yesterday’s date, use the command:
(Get-Date).AddDays(-1)
To get tomorrow’s date:
(Get-Date).AddDays(1)
To get the first day of the current month, use the command:
Get-Date -Day 1
To get the last day of the current month, use the following:
The command mentioned above returns a unique file name that includes the current date and time. This naming format is commonly used for backup files, reports, and log rotation tasks.
Performance Considerations
When processing large collections of objects, you should avoid calling Get-Date repeatedly inside loops. Instead, you need to retrieve the current date once and reuse the variable. This can reduce unnecessary cmdlet invocations and improve script performance. Here is the example:
$CurrentDate = Get-Date
foreach ($User in $Users) { if ($CurrentDate -gt $User.ExpirationDate) { # Process user } }
Get-Date retrieves the current date and time and can be used to format dates, perform date calculations, compare dates, generate timestamps, and work with time zones.
No. When you use the -Format parameter, PowerShell returns a string rather than a DateTime object.ย If you need to perform calculations, keep the value as a DateTime object and apply formatting only when displaying the result.
I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.