Getting a timestamp
In this recipe, we simply get the system's current timestamp.
How to do it...
This is how we will get the timestamp.
Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell.
Add the following script and run it:
$timestamp = Get-Date -Format "yyyy-MMM-dd-hhmmtt" #display timestamp $timestamp
Following is a sample result:
How it works...
Often, we find ourselves needing the timestamp to append to different files we create or modify. To get the timestamp in PowerShell, we simply have to use the Get-Date
cmdlet, which gives the following default format:
To change the
format, we can use the –Format
switch, which accepts a format string. In our recipe, we used the following format: "yyyy-MMM-dd-hhmmtt
".
There are a number of standard format strings that return preformatted datetime type, or you can also compose your own format string. Common format strings, as documented in MSDN are as follows:
Format Pattern |
Description |
---|---|
|
AM/PM designator |
|