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.
Using the Get-Date Command The number one command to consider when working with dates and times in PowerShell is the Get-Date command. By running Get-Date just by itself, you will output a DateTime object with the current date and time. Using the Get-Member command, we can see that the output is a System.
dd/MM/yyyy — Example: 23/06/2013. yyyy/M/d — Example: 2013/6/23. yyyy-MM-dd — Example: 2013-06-23. yyyyMMddTHH:mmzzz — Example: 20130623T13:22-0500.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With