Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to get the system date.
To get the system date, run the command Get-Date
.
The Get-Date
command generates rich object-based output, so you can use its result for many date-related tasks. For example, to determine the current day of the week:
PS > $date = Get-Date PS > $date.DayOfWeek Sunday
If you want to format the date for output (for example, as a logfile stamp), see Recipe 5.13.
For more information about the Get-Date
cmdlet, type Get-Help Get-Date
.
For more information about working with classes from the .NET Framework, see Recipe 3.8.
Recipe 3.8, “Work with .NET Objects”
Recipe 5.13, “Format a Date for Output”