1

Get-Date -format yyyy/M/d works in PS1 but it doesnt work with variable date like, $date -format yyyy/M/d. It prompts an error - Unexpected token in expression or statement.

How can I make the $date variable work.

Thanks, A curious mind!

anuj khosla
  • 43
  • 1
  • 1
  • 5

2 Answers2

3

You can recreate an object with Get-Date:

Get-Date $date -format yyyy/M/d
sodawillow
  • 11,431
  • 4
  • 34
  • 43
2

You have to use ($date).ToString('yyyy/M/d')

Editing to say you can pipe the $date variable to Get-Member in order to reveal the available methods. Then we can search for formatting a DateTime string in PowerShell For example, this question

$date | GM
TypeName: System.DateTime
ToString             Method         
 string ToString(), string ToString(string format), 
 string ToString(System.IFormatProvider provider), ...
Community
  • 1
  • 1
user4317867
  • 2,274
  • 4
  • 27
  • 55