Get-MyAge.ps1


<#PSScriptInfo
 
.VERSION 1.1
 
.GUID 72e37320-04fb-4df9-9ba1-815108578691
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT saw-friendship
 
.TAGS saw-friendship My Age
 
.LICENSEURI
 
.PROJECTURI https://sawfriendship.wordpress.com/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Measure your Age
 
.LINK
 https://sawfriendship.wordpress.com/
 
.EXAMPLE
 Get-MyAge 1969.06.30
 
.EXAMPLE
 Get-MyAge 1969.06.30 -SaveMyBirthDay
 
 
#>
 


param(
[datetime]$WasBorn,
[datetime]$ToDay = (Get-Date).Date,
[switch]$SaveMyBirthDay
)
    
    $FilePath = "$env:APPDATA\Microsoft\Windows\PowerShell\Get-MyAge\MyBirthDay"
    
    if($SaveMyBirthDay -and $WasBorn){
        New-Item -Type File -Path $FilePath -Value $WasBorn.ToString('yyyy-MM-dd') -Force | Out-Null
    }
    if(!$WasBorn){
        if((Test-Path $FilePath  -ErrorAction SilentlyContinue)){
            $WasBorn = [datetime]::ParseExact($(Get-Content -Path $FilePath -ErrorAction SilentlyContinue),'yyyy-MM-dd',$NULL).Date
        } else {
            $WasBorn = (Get-Date).AddYears(-18).Date
        }
        
    }
    
    Foreach ($Year in ($WasBorn.Year .. $ToDay.Year)) {
        $BirthDay = $WasBorn | Get-Date -Year $Year
        if($BirthDay -le $ToDay){
            $BirthDays += @($BirthDay)
        }
    }
    [pscustomobject][ordered]@{
    'FullYears' = [int]$($BirthDays.Count - 1)
    'Age' = [string]$("$($BirthDays.Count - 1) Y & $( ($ToDay - $BirthDays[-1]).TotalDays ) d")
    'FirstBirthDay' = $WasBorn.ToString('yyyy-MM-dd')
    'FirstBirthDayOfWeek' = $WasBorn.DayOfWeek
    'LastBirthDay' = $BirthDays[-1].ToString('yyyy-MM-dd')
    'LastBirthDayOfWeek' = $BirthDays[-1].DayOfWeek
    'NextBirthDay' = $BirthDays[-1].AddYears(1).ToString('yyyy-MM-dd')
    'NextBirthDayOfWeek' = $BirthDays[-1].AddYears(1).DayOfWeek
    'NextBirthDaysRemaining' = [int]$(($BirthDays[-1].AddYears(1) - $ToDay).TotalDays)
    'ToDay' = $ToDay.ToString('yyyy-MM-dd')
    }