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
  • 6

2 Answers2

4

You can recreate an object with Get-Date:

Get-Date $date -format yyyy/M/d
sodawillow
  • 12,497
  • 4
  • 34
  • 44
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,397
  • 4
  • 31
  • 57