#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Convert-InstallDate.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 1/11/2011 # Keywords: WMI, WIn32_Product, DateTime # Comments: Convert a datetime string like InstallDate from Win32_Product into # a PowerShell date time object. # # PS C:\>Convert-InstallDate "20100415" # Thursday, April 15, 2010 12:00:00 AM # # "Those who neglect to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Convert-InstallDate { Param( [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, HelpMessage="Enter a 8 character datetime string in the format YYYYMMDD")] [ValidateLength(8,8)] [Alias("InstallDate")] [string[]]$DateString ) Process { Foreach ($string in $datestring) { Write-Verbose "Parsing $string" #parse date values from string $year=$string.Substring(0,4) $month=$string.Substring(4,2) $day=$string.Substring(6) #build a datetime object Write-Verbose "$month/$day/$year" [datetime]$InstallDate="$month/$day/$year" #write the object to the pipeline Write-Output $InstallDate } } } #end function