Search code examples
datepowershellformatting

How do I get 'date-1' formatted as mm-dd-yyyy using PowerShell?


How does one get date-1 and format it to mm-dd-yyyy in PowerShell?

Example: If today is November 1, 2013, and I need 10-31-2013 in my code.

I've used AddDays(-1) before, but I can't seem to get it to work with any formatting options.


Solution

  • You can use the .tostring() method with datetime format specifiers to format to whatever you need:

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

    (Get-Date).AddDays(-1).ToString('MM-dd-yyyy')
    11-01-2013