Difference between revisions of "Power Shell"

Jump to navigation Jump to search
809 bytes added ,  15:09, 24 February 2011
→‎Formatting: Added ToString
(Offloaded stuff to "PowerShell Examples")
(→‎Formatting: Added ToString)
Line 304: Line 304:


==== Formatting ====
==== Formatting ====
To control how a DateTime is displayed you can pass it through <code> Get-Date </code> with the <code> -uFormat </code> option...
To control how a DateTime is displayed you can pass it through <code> Get-Date </code> with the '''<code> -uFormat </code>''' option...
<source lang="powershell">Get-Date $datetime -uFormat "%R hrs, %a %d %b %Y"</source>
<source lang="powershell">Get-Date $datetime -uFormat "%R hrs, %a %d %b %Y"</source>


Line 310: Line 310:
{|cellpadding="2" cellspacing="0" border="1"
{|cellpadding="2" cellspacing="0" border="1"
|- style="background-color:#bbddff;"
|- style="background-color:#bbddff;"
! Specifier                            !! Example
! uFormat Specifier                            !! Example
|-
|-
| <code> %R hrs, %a %d %b %Y </code>    || <code> 07:25 hrs, Fri 24 Dec 2010 </code>
| <code> %R hrs, %a %d %b %Y </code>    || <code> 07:25 hrs, Fri 24 Dec 2010 </code>
Line 316: Line 316:
| <code> %Y-%m-%d </code>              || <code> 2010-12-24 </code>
| <code> %Y-%m-%d </code>              || <code> 2010-12-24 </code>
|}
|}
For the full list of formatting options see http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx


For the full list of formatting options see http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx


Or you can use the '''<code>ToString</code>''' method provided by the object, to convert the date.  If you use a generic specifier you'll get a local format output...
<source lang="powershell">$datetime.ToString("s")</source>
Useful formatting examples...
{|cellpadding="2" cellspacing="0" border="1"
|- style="background-color:#bbddff;"
! ToString Method Specifier          !! UK Example
|-
| <code> <blank> </code>            || <code> 17/02/2011 13:07:33 </code>
|-
| <code> d </code>                  || <code> 17/02/2011 </code>
|-
| <code> D </code>                  || <code> 17 February 2011 </code>
|-
| <code> yyyy-MM-dd HH:mm:ss </code> || <code> 2011-02-17 13:07:33 </code>
|}
For the full list of formatting options see http://technet.microsoft.com/en-us/library/ee692801.aspx


==== Improper (US) Formatting ====
==== Improper (US) Formatting ====

Navigation menu