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 as an administrator.
- Add the following script and run it:
$timestamp = Get-Date -Format "yyyy-MMM-dd-hhmmtt" #display timestamp $timestamp
Here 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 this recipe, we used the yyyy-MMM-dd-hhmmtt
format.
There are a number of standard format strings that return preformatted datetimes, or you can also compose your own format string. The common format strings, as documented in MSDN, are as follows:
Format pattern |
Description |
---|---|
|
AM/PM designator |
|
Seconds with leading zero |
|
Minutes... |