PowerTip: Displaying Only the Current Year in PowerShell

PowerTip: Displaying Only the Current Year in PowerShell

  • Comments 9
  • Likes

Summary: Learn how to display only the current year.

Hey, Scripting Guy! QuestionHow can I use Windows PowerShell to display only the current year?

Hey, Scripting Guy! Answer

  1. get-date -Format yyyy

  2. get-date –format yy

  3. get-date –f yyyy

  4. (Get-Date).year

  5. Get-Date -UFormat %Y

  6. Get-Date -UFormat %

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>&quot;Double, double toil and trouble; Fire burn, and caldron bubble&quot;</p> <p>It&#39;s witchcraft...</p> <p>More magic from everybodies favorite mad scientist.</p>

  • <p>@jrv: Magic Ed Wilson @ work ...</p> <p>The wizard of South Carolina :-)</p> <p>As Teresa told me, &quot;Date and time voodoo&quot; is one of his hobbies</p> <p>besides diving, wood working and (underwater) photography</p> <p>Klaus.</p>

  • <p>Hi</p> <p>for fun:</p> <p>PS&gt; ( date ).ToString(&#39;yyyy&#39;)</p> <p>PS&gt; [datetime]::Today.Year</p> <p>PS&gt; (Get-WmiObject Win32_LocalTime).Year</p> <p>PS&gt; </p>

  • <p>@Walid - I&#39;ll see you and raise you one:</p> <p>(date).Year</p>

  • <p>@JRV hehehe</p> <p>@K_Schulte thanks ... </p> <p>@Walid Toumi these are great ... thanks for sharing.</p> <p>@JRV cool. Thanks. </p> <p>@JRV @Walid Toumi can you explain WHY Date works? It is not a cmdlets, nor it is a function or an alias!</p>

  • <p>@IamMred</p> <p>all cmdlet with verb &quot;get&quot; works</p> <p>childitem</p> <p>item</p> <p>service</p> <p>only get-process and get-command don&#39;t work</p> <p>because proccess is a special keyword &quot;help keyword&quot; </p> <p>and command is old shell win command.com</p> <p>sorry for my english</p>

  • <p>@Walid - good explanation and your english is fine.</p> <p>I learned somthing here today. &nbsp;Thank you Walid and Ed.</p> <p>What you have pointed out is this:</p> <p>PS&gt;function get-year{date -uf %Y}</p> <p>PS&gt;year</p> <p>2012</p>

  • <p>And remember that these might give a different result to</p> <p>[DateTime]::UtuNow.Year</p>

  • <p>@all one of the great things about PowerShell is that is simplifies working with dates. In other languages I have used, datetime manipulation was always a major pain. I still remember old databases that had one column for month, another column for day, and another column for year. UGH!!! </p>