Home » PowerShell » Working With PowerShell Date Variable

Working With PowerShell Date Variable

Date and Time are essential for scheduling, file management, and logging in PowerShell scripts. The Get-Date cmdlet is used to create a date variable in PowerShell and initialize the date and time value to a variable.

In this tutorial, we will discuss how to create a date variable in PowerShell, format date and time variables, and use the date-time variables in scripts.

Create and Initialize Date and Time Variables in PowerShell

Use the Get-Date cmdlet in PowerShell to create and initialize date and time variables with datetime or you can specify a date string to create a variable.

$todayDate = Get-Date

The above PowerShell script uses the Get-Date cmdlet to assign the current date to a variable $todayDate.

You can specify the date string to create a variable:

$specificDate = [datetime]"2023-04-02"

The output of the above PowerShell scripts creates and initializes date time variables.

Format Date and Time Variables

While creating and assigning a datetime value to a variable, you can specify the format. PowerShell provides different formatting options for date and time variables.

$todayDate = Get-Date
$tempDate = $todayDate.ToString("yyyy-MM-dd")
$tempDate1 = "{0:yyyy/MM/dd}" -f $todayDate

In the above PowerShell script, the variable $todayDate holds the current datetime. To format datetime variables, we have used ToString() method or the -f format specifier.

The output of the above PowerShell script to format date time variables is:

PS C:\> $todayDate = Get-Date                                                                                           PS C:\> $tempDate = $todayDate.ToString("yyyy-MM-dd")                                                                   PS C:\> $tempDate                                                                                                       
2023-04-08

PS C:\> $tempDate1 = "{0:yyyy/MM/dd}" -f $todayDate                                                                     PS C:\> $tempDate1                                                                                                      2023-04-08

PS C:\>     

Using Date and Time Variables in Scripts

The date and time variable holds the datetime value and can be easily used in scripts or commands.

$todayDate = Get-Date  
Write-Host "Today is $($todayDate.ToShortDateString())"
Write-Host "The current time is $($todayDate.ToLongTimeString())"

In the above PowerShell script, the variable $todayDate stores the current datetime value. It has been used with the Write-Host cmdlet to print today’s date and the current time.

The output of the above PowerShell script is:

PS C:\> $todayDate = Get-Date    
                                                                                       
PS C:\> Write-Host "Today is $($todayDate.ToShortDateString())"                                                         Today is 08-04-2023

PS C:\> Write-Host "The current time is $($todayDate.ToLongTimeString())"
The current time is 12:49:42

PS C:\>                                                                                                                             

Best Practices for Working with DateTime Variables in PowerShell

While creating a datetime variable in PowerShell, use the meaningful variable names for better readability and maintainability of scripts.

Always validate the user input before assigning the value to date time variables.

While doing a comparison or operation with datetime variables, ensure the time zones, and daylight saving time.

Conclusion

I hope the above article on how to create a PowerShell date variable and effectively used it in the script is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.