Fighting with dates in Powershell

  • Hi,

    I am trying to get a yesterday's date into a variable:

    $timestamp = (Get-Date).AddDays(-1) -Format yyyy-MM-dd

    This errors, so I have to use AddDays method without the Format option:

    $timestamp = (Get-Date).AddDays(-1)

    $DBScriptFile = "\\MyServer\Scripts\MyDb\08_SPs " + $timestamp.Year + '-' + $timestamp.Month +'-' + $timestamp.Day + ".sql"

    But I get:

    \\MyServer\Scripts\MyDb\08_SPs 2011-11-1.sql

    But I need to get:

    \\MyServer\Scripts\MyDb\08_SPs 2011-11-01.sql

    I want to get the desired format without going into too much complexity in script logic.

    Any ideas?

  • Found it out:

    $timestamp = (get-date).AddDays(-1);

    $timestamp = date $timestamp -format yyyy-MM-dd

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply