How to use PowerShell get-date cmdlets (PowerShell get date)

In this PowerShell tutorial, we will discuss how to use PowerShell get-date cmdlets with various examples of PowerShell get-date command. Let us see various examples on PowerShell get date.

PowerShell get-date cmdlets

Now, we will see how to use PowerShell get-date cmdlets or the PowerShell date function.

Get-Date: The PowerShell Get-Date command is used to display the system’s current date and time.

Syntax: Below is the syntax of PowerShell Get-Date command.

Get-Date
   [[-Date] <DateTime>]
   [-Year <Int32>]
   [-Month <Int32>]
   [-Day <Int32>]
   [-Hour <Int32>]
   [-Minute <Int32>]
   [-Second <Int32>]
   [-Millisecond <Int32>]
   [-DisplayHint <DisplayHintType>]
   [-Format <String>]
   [<CommonParameters>]

You can use the Windows PowerShell or PowerShell ISE to debug, execute, and test script on PowerShell get date.

Get-Date
PowerShell get-date
powershell get date

This is how we can use powershell get date to retrieve the current date and time.

You can also use the below command to get the system datetime settings.

(Get-Culture).DateTimeFormat

PowerShell get-date format

Now, we will see some PowerShell get-date format examples.

Display the current date only

From the get-date cmdlet, you can retrieve get the only Date, Time, or DateTime only.

PowerShell provides -DisplayHint parameter which we can use to get the date only, time only, or DateTime only.

Below is the PowerShell cmdlet to get current date only.

Get-Date -DisplayHint Date

Display the current time Only

By using the below PowerShell command to display the current time only.

Get-Date -DisplayHint Time

Display the current datetime Only

Below is the PowerShell cmdlet to get the System’s current datetime.

Get-Date -DisplayHint DateTime
Get Only Date from get-date
powershell get date

Get a date’s day of the year

This is an interesting command to get a date’s day of the year.

It will display the day of the year by using the DayofYear property like below:

(Get-Date -Year 2020 -Month 12 -Day 31).DayOfYear
(Get-Date -Year 2020 -Month 12 -Day 5).DayOfYear
(Get-Date -Year 2019 -Month 05 -Day 10).DayOfYear

The output will appear like below:

Get a date's day of the year
powershell get date example

Since 2020 is the leap year, you can see it is returning 366.

How to display only a month of the year from get-date

We can easily get-date formatting to display only a month of the year.

(Get-Date).Month

Apart from this we can also get Year, Day, DayOfWeek, DayOfYear from the get-date cmdlets in PowerShell etc.

(Get-Date).Month
(Get-Date).Year
(Get-Date).Day
(Get-Date).DayOfWeek
(Get-Date).DayOfYear

You can see the output like below:

PowerShell get-date format
powershell get date examples

PowerShell get-date format examples

Here are few more examples of how we can format PowerShell get-date cmdlets.

(get-date).ToString("d.M.yyyy hh:mm tt")

The above PowerShell cmdlet displays the current date in a date.month.year and hour.minute AM/PM.

By using “tt” we will get the current time in the am and pm format.

Get-Date -Format g

This Get-Date-Format g command displays the current date and current time in a short format.

Get-Date -Format t

The Get-Date -Format t command will display the only the time in a short format

Get-Date -Format d

The Get-Date -Format d display only date in short format.

Get-Date -Format U

The Get-Date -Format U cmdlets is used to display the full date and time using the universal time.

You can see the output looks like below:

PowerShell get-date format examples
get-date | fl *

The above command will dsiplay the properties like DateTime, Date, Day, DayOfWeek, DayOfYear, Hour, Kind etc in the list format.

get-date list format
(get-date).ToUniversalTime()

The PowerShell cmd will convert current date time to universal time zone(UTC).

Check if a date is adjusted for daylight savings time

By using the below cmdlets, you can check if a date is adjusted for daylight savings time or not.

We can use the IsDaylightSavingTime method to check if the date is adjusted for daylight savings time.

So it will return true/false.

$isdatetime = Get-Date
$isdatetime.IsDaylightSavingTime()
Check if a date is adjusted for daylight savings time

Convert the current time to UTC time

We can easily convert the current time to UTC time easily from the Get-Date.

$currentTime = Get-Date
$currentTime.ToUniversalTime()

The above command will easily convert the current time to UTC time.

Convert the current time to UTC time

PowerShell get-date adddays

We can use PowerShell get-date adddays to add a number of days to the current date.

Syntax:

(get-date).AddDays(2)
PowerShell get-date adddays

You can see the above cmdlets displays date by adding 2 days into today’s date.

Apart from adddays, we can also use PowerShell get-date AddHours, AddMinutes, AddMonths, AddSeconds, AddYears etc.

(get-date).AddDays(2)
(get-date).AddMonths(2)
(get-date).AddYears(2)
(get-date).AddSeconds(40)
(get-date).AddMinutes(10)
(get-date).AddHours(5)

You can see the output like below:

PowerShell get-date adddays or AddMonths example

PowerShell get-date yesterday

We can also get yesterday’s date by using the same PowerShell get-date AddDays method by passing -1 like below:

(get-date).AddDays(-1)
PowerShell get-date yesterday

This is how we can get yesterday’s value using PowerShell get-date.

PowerShell get-date add hours

We can use the PowerShell get-date AddHours method to add a number hours to current date like below:

(get-date).AddHours(5)

You can see it is adding 5 hours to current time and displaying.

PowerShell get-date add hours

PowerShell get-date timezone

By using the Get-TimeZone, we can get the System’s timezone.

Get-TimeZone

The above PowerShell cmdlet will display, the system’s current time zone.

PowerShell get-date timezone

We can also set the system’s timezone to another timezone by using the below PowerShell cmdlet.

Set-TimeZone -Name "Pacific Standard Time"

PowerShell get-date from string

We can also convert a string to date in PowerShell like below:

$stringdate = '29-Apr-2020'
$stringfinaldate=[datetime]::parseexact($stringdate, 'dd-MMM-yyyy', $null)
$stringfinaldate

You can see the output will appear like below:

PowerShell get-date from string

PowerShell set-date

We can also set the local system’s date to a specific date by using the PowerShell set-date cmdlet, check below the PowerShell cmdlet.

Set-Date -Date "06/03/2019 18:53"
PowerShell set-date
PowerShell set-date

PowerShell get-date ToShortDateString()

By using PowerShell get-date ToShortDateString() method to get today’s date in short date format like below:

(get-date).ToShortDateString()

Apart from this you can also overrride the (get-date).ToString() method to display today’s date in various formats like below:

(get-date).ToString("dd-MM-yyyy")
(get-date).ToString("dd-MMM-yyy")
get-date ToShortDateString format

Write Current Date to a text file in PowerShell

In PowerShell, we can write current date to a text file by using the below command.

Get-Date| Add-Content -Path "G:\padmini\New folder\PSDate.txt"

This command will write the current date to a text file.

Write Current Date to a text file in PowerShell
create a text file in PowerShell

PowerShell get difference between two dates in days

We can easily get the difference between two dates in days using PowerShell.

$startdate = '20-Apr-2020'
$startdate=[datetime]::parseexact($startdate, 'dd-MMM-yyyy', $null)

$enddate = '25-May-2020'
$enddate=[datetime]::parseexact($enddate, 'dd-MMM-yyyy', $null)

$datediff=$enddate-$startdate
$datediff

It will display in details like Days, Hours, Minutes, Seconds etc. the difference between two days.

PowerShell get difference between two dates in days
PowerShell get date difference between two dates in days

If you want to get just the difference between two dates in days in PowerShell, you can use like below:

$datediff.Days
$datediff.TotalDays

You can see the result like below:

powershell get difference between two dates

PowerShell get date and time in .NET format

If you have little knowledge on Microsoft.Net, then it will be easier for you to format the PowerShell get date and time into the various useful string format.

You can see a few examples below uses the .Net formats.

Get-Date -Format "dd-MM-yyyy"
Get-Date -Format "dddd-MM-yyyy"
Get-Date -Format "dddd-MMM-yyyy"
Get-Date -Format "dd-MM-yyyy HH:mm"
Get-Date -Format "MM-dd-yyyy"
Get-Date -Format "MM-dd-yyyy HH:mm"

You can see the output:

powershell get date
PowerShell get date

You may like following PowerShell tutorials:

In this PowerShell tutorial, we discussed how to use PowerShell get-date cmdlets. We also discussed the below things:

  • PowerShell get-date cmdlets
  • PowerShell get-date format
  • Display the current date only
  • Display the current time Only
  • Display the current datetime Only
  • Get a date’s day of the year
  • How to display only a month of the year from get-date
  • PowerShell get-date format examples
  • Check if a date is adjusted for daylight savings time
  • Convert the current time to UTC time
  • PowerShell get-date adddays
  • PowerShell get-date yesterday
  • PowerShell get-date add hours
  • PowerShell get-date timezone
  • PowerShell get-date from string
  • PowerShell set-date
  • PowerShell get-date ToShortDateString()
  • Write Current Date to a text file in PowerShell
  • Powershell get difference between two dates in days
  • >