To get a date without Time in PowerShell, you can use the Get-Date cmdlet with the custom format string.
There are multiple ways, you can get a date without time in PowerShell, the following method shows how you can do it.
Method 1: Using Get-Date cmdlet with custom format
Get-Date -Format "dd-MM-yyyy"
This will output the current date without time in the format “dd-MM-yyyy“.
Method 2: Using the [DateTime] class with the ToString() method
[DateTime]::Today.ToString("dd-MM-yyyy")
This will output the current date without time in the format “dd-MM-yyyy“.
Method 3: Using the Get-Date and ToString with a custom format
(Get-Date).Date.ToString("dd-MM-yyyy")
This will output the date without the time component.
The following examples show how you can use these methods with syntax.
Using Get-Date with Format Property
The following PowerShell script shows how you can use the Get-Date cmdlet with the format property to get a date without time.
Get-Date -Format "dd-MM-yyyy"
Output:
17-04-2024
In this script, the Get-Date cmdlet in PowerShell uses the -Format property to specify the date format “dd-MM-yyyy“.
After running the script, it will output the current date without the time.
Using [DateTime]::Today to Get Date Without Time
You can use the [DateTime]::Today to get the date without the time component. The following example shows how you can do it with syntax.
[DateTime]::Today.ToString("dd-MM-yyyy")
Output:
17-04-2024
In this PowerShell script, the [DateTime].Today returns the current date. It uses the ToString() method to specify the custom format (“dd-MM-yyyy”) to get the date without time.
After running the script, it will output the date in the format of “dd-MM-yyyy” without a time component.
Using the Get-Date and ToString Method
Another way to get a date without time is by using the Get-Date cmdlet in PowerShell with the ToString() method to specify the format.
(Get-Date).Date.ToString("dd-MM-yyyy")
Output:
17-04-2024
The above PowerShell script displays the current date without time.
Conclusion
I hope the above article on getting the date without time in PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.