PowerShell date format mmddyy - date

I know I can set a date to a variable in PowerShell using
$a = Get-Date
It works great, but when looking through the documentation on formatting the date, there wasn't one for MMDDYY.
How do I accomplish this?

Because $a is effectively a System.DateTime. You can do:
$a = Get-Date
Write-Host $a.ToString('MMddyy')
Full details on custom Date and Time Format Strings are in Custom Date and Time Format Strings.

You just have to get creative with the -UFormat options. To get exactly what you want run this:
(Get-Date -UFormat %m)+(Get-Date -UFormat %d)+(Get-Date -UFormat %y)
However I think this is much easier to read:
Get-Date -UFormat %D

Related

PowerShell - Special Date Format

I'd like to have an output of get-date for the current day in PowerShell to the following format:
11-Apr-2018
I've tried playing around with this and have not been successful. Ideas?
Get-Date -format "dd-MMM-yyyy"
Formatting strings follow .NET standards.
See also official docs here.
If you did a little bit of research, you will find a list with all the date format patterns and the description to them aswell
here https://ss64.com/ps/syntax-dateformats.html
In your case
Get-Date -format "dd-MMM-yyyy"

PowerShell: different result with and without string interpolation (with Get-Date -DisplayHint Time)

When I run Get-Date -DisplayHint Time, the output is 10:30:19.
When I run "$(Get-Date -DisplayHint Time)", the output is 02/15/2018 10:30:15.
Why the difference?
PSVersion = 5.1.16299.98
Because -DisplayHint is, well, a display hint. The result of the cmdlet is still a DateTime object. Inside a string the expression doesn't count as being "displayed", and you will get... something else. (Curiously, it's not the result of a simple .ToString()). Use "$(Get-Date -Format 'T')" if you want the locale-dependent long time format inside a string (which is apparently what -DisplayHint Time will do, although that's not explicitly documented as such).

Change of format Get-Date` PowerShell

How to do Get-Date to show me this format 2018-01-10 and that does not show the time?
I was currently using Get-Format s but it shows me time.
There are several ways to do this. For instance you could use the -Format parameter of the cmdlet:
Get-Date -Format 'yyyy-MM-dd'
You could use the ToString() method of the DateTime object the cmdlet produces:
(Get-Date).ToString('yyyy-MM-dd')
You could also use PowerShell's format operator (-f):
'{0:yyyy-MM-dd}' -f (Get-Date)
Other method (without get-date):
[System.DateTime]::Today.ToString("yyyy-MM-dd")
Powershell Script to get date as requested
get-date -Format yyyy-MM-dd
Following doc will give you brief explanation on formats
https://technet.microsoft.com/en-us/library/ee692801.aspx

How can I use get-date to change the month while controlling the format in Powershell

I just started working with Powershell and I am trying to manipulate a date such that I add two months to the date and set it as a var. Powershell handles this nicely and even handles year rollover. However I am stuck trying to figure out how to also control the format of the date before, during or after adding two months to the date. Both of the statements below give me what I want but i have not been able to figure out how to combine them.
$ndate = (Get-Date).AddMonths(2)
$exp = date -format MM/dd/yyyy
Thank you,
Get-Date (Get-Date).AddMonths(2) -f MM/dd/yyyy
When the -Format operator is not avaiable you can use the ToString method:
(Get-Date).AddMonths(2).ToString('MM/dd/yyyy')

Writing powershell functions within a string

If I have:
Write-Host "[$(Get-Date -displayhint time)] backup starting..."
I get:
[02/17/2010 1:26:12pm] backup starting...
i. e. the Get-Date parameters are being ignored and is just returning the output of Get-Date.
What's the best way to do inject the current time in the middle of a string?
thanks
Well, in this case you're converting to a string because you are using the output in a string. The result of the Get-Date command is still a DateTime object. The display hint would then be honored by the Out-Host cmdlet.
You can use the -Format parameter to force a certain format in which case the cmdlet returns a string:
Get-Date -Format T
("T" being the format string for the full time) which then looks like this:
PS Home:\> Write-Host "[$(Get-Date -Format T)] backup starting..."
[19:35:12] backup starting...
You can also use the ToString() method of the DateTime class. I usually do something like this:
[PS] C:\>write-host "$((get-date).tostring('yyyy.MM.dd-HH:mm:ss'))"
2010.02.19-09:54:51