locked
Powershell Date and Echo RRS feed

  • Question

  • Hello,

    I am trying to echo the date to a file so i can basically just record the last day the script was ran.  I do not what the exact time, but just i am happy with dates.

    Anyways i simply do this:

    $DATE = get-date -DisplayHint date

    echo $DATE >> backupdate.txt

    However when looking at the file it outputs a blank line before and 3 blank lines after. I simply want the script to write the date on a new line.  so the 3 blank lines afterwards is a bit excessive.  So i just want this in the file

    Monday, February 10, 2014
    Monday, February 11, 2014
    Monday, February 12, 2014

    How can i fix this formatting issue? 


    Monday, February 10, 2014 6:49 PM

Answers

  • Those extra blank lines seem to be coming from PowerShell's default formatting.  You can convert the DateTime object to a string yourself, at which point that's all that goes to the file:

    $DATE.ToString('D') >> backupdate.txt

    Monday, February 10, 2014 6:55 PM

All replies

  • Those extra blank lines seem to be coming from PowerShell's default formatting.  You can convert the DateTime object to a string yourself, at which point that's all that goes to the file:

    $DATE.ToString('D') >> backupdate.txt

    Monday, February 10, 2014 6:55 PM
  • Another useful method when you cannot remember the exact for4mat string is this:

    [datetime]::Today.ToShortDateString()

    You can tab through all of the choices at the prompt startting with this:

    [datetime]::Today.To<tab>...


    ¯\_(ツ)_/¯

    Monday, February 10, 2014 8:20 PM