Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Open PowerShell ISE as an administrator.

  2. 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

tt

AM/PM designator

ss

Seconds with leading zero

mm

Minutes with leading zero

dd...