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-DateSample output:
Tuesday, September 5, 2023 9:46:02 AMYou 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:02Using .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]::NowSample output:
Tuesday, September 5, 2023 9:46:02 AMFor UTC time:
# Using .NET DateTime class to get the current UTC date and time
[System.DateTime]::UtcNowSample output:
Tuesday, September 5, 2023 1:46:02 PMThese commands and classes provide powerful and flexible options for working with dates and times in PowerShell, essential for many scripting and automation tasks.