Functions/Test-IsDate.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
function Test-IsDate { <# .SYNOPSIS Tests to see if the specified string is a valid [datetime] string .DESCRIPTION Tests to see if the specified string is a valid [datetime] string. Can accept a string or an array of strings. Can also accept pipeline input. .EXAMPLE Test-IsDate Would return $null .EXAMPLE Test-IsDate 1/1/19 -Verbose Would return: VERBOSE: The string you entered is [1/1/19] True .EXAMPLE Test-IsDate @('1/1/1965','2/1/19','dne', '3 Feb 2019 17:00') -IncludeInput Would return Input Result DateTime ----- ------ -------- 1/1/1965 True 1/1/1965 12:00:00 AM 2/1/19 True 2/1/2019 12:00:00 AM dne False 3 Feb 2019 17:00 True 2/3/2019 5:00:00 PM #> #region Param [CmdletBinding(ConfirmImpact='None')] [OutputType('bool')] Param ( [parameter(ValueFromPipeLine,ValueFromPipeLineByPropertyName)] [Alias('date')] [string[]] $DateString, [switch] $IncludeInput ) #endregion Param begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { foreach ($d in $DateString) { try { Write-Verbose -Message "The string you entered is [$DateString]" [DateTime] $d | Out-Null if ($IncludeInput) { New-Object -TypeName psobject -Property ([ordered] @{Input="$d";Result=$true; DateTime=[DateTime] $d}) } else { Write-Output -InputObject $True } } catch { if ($IncludeInput) { New-Object -TypeName psobject -Property ([ordered] @{Input="$d";Result=$false; DateTime=$null}) } else { Write-Output -InputObject $false } } } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } #EndFunction Test-IsDate |