mhouston100
mhouston100

Reputation: 1203

Powershell - Easier way to get short date for filename

More out of curiosity as its working fine for me (I'm just beginning with Powershell) but I'm using below to obtain a short date for use in file names:

$shortdate = (get-date).toshortdatestring().replace("/",".")

Is there a built in functionality for this or is this the correct way to accomplish this?

Upvotes: 3

Views: 7629

Answers (2)

zdan
zdan

Reputation: 29450

You can use the -Format argument of get-date:

get-date -format "MM.dd.yyyy"
11.20.2012

Upvotes: 6

alroc
alroc

Reputation: 28174

This can be done very similarly to how it's done in C#.

$shortdate = (Get-Date).ToString("yyyy.MM.dd")

Your method is somewhat dependent upon localization - if you run that same code on a system where the date format doesn't use / as a delimiter, it breaks.

Upvotes: 3

Related Questions