PowerShell:
Getting the current date

How to:

PowerShell provides straightforward cmdlets for getting the date and time. The Get-Date cmdlet is the primary tool for this purpose. It can return the full date and time, which you can format or manipulate according to your needs.

# Get the current date and time
Get-Date

Sample output:

Tuesday, September 5, 2023 9:46:02 AM

You can also format the output to display only the information you need, such as just the date or just the time.

# Get only the current date in a specific format
Get-Date -Format "yyyy-MM-dd"

Sample output:

2023-09-05
# Get only the current time
Get-Date -Format "HH:mm:ss"

Sample output:

09:46:02

Using .NET Class

PowerShell allows direct access to .NET classes, offering an alternative way to work with dates and times.

# Using .NET DateTime class to get the current date and time
[System.DateTime]::Now

Sample output:

Tuesday, September 5, 2023 9:46:02 AM

For UTC time:

# Using .NET DateTime class to get the current UTC date and time
[System.DateTime]::UtcNow

Sample output:

Tuesday, September 5, 2023 1:46:02 PM

These commands and classes provide powerful and flexible options for working with dates and times in PowerShell, essential for many scripting and automation tasks.