Get-WindowsVersion.psm1

function Get-WindowsVersion {
    <#
.SYNOPSIS
    List current or History Windows Version from local computer.
.DESCRIPTION
    List current or History Windows Version from local computer.
.PARAMETER History
    List History Windows Version from computer.
.EXAMPLE
    Get-WindowsVersion
    List Windows Version on local computer.
.EXAMPLE
    Get-WindowsVersion -History.
    List History Windows Version on local computer.
.EXAMPLE
    Get-WindowsVersion -History | Sort-Object WindowsVersion -Descending
    List History Windows Version on local computer with descending sorting from WindowsVersion.
#>

    param (
        [switch] $History
    )
    # OS Match
    if (($PsVersionTable.OS -match "10.0") -or ($PsVersionTable.BuildVersion -match "10.0")) {
        # WindowsVersionArray
        $WindowsVersionArray = @()
        $WindowsVersionArray += Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
        $WindowsVersionArray += ForEach-Object {
            $SourceOSName = (Get-ChildItem -Path "HKLM:\SYSTEM\Setup\" | Where-Object Name -Match "Source OS").Name
            if ($Name) { Get-ItemProperty -Path ($SourceOSName -replace "HKEY_LOCAL_MACHINE", "HKLM:") }
        }
        # WindowsVersionObject
        $WindowsVersionObject = $WindowsVersionArray | ForEach-Object {
            # Variables
            $Build = $_.CurrentBuildNumber
            $DisplayVersion = $_.DisplayVersion
            $InstallTime = ([datetime]::FromFileTime($_.InstallTime))
            $Major = $_.CurrentMajorVersionNumber
            $Minor = $_.CurrentMinorVersionNumber
            $ProductId = $_.ProductId
            $ProductName = $_.ProductName
            $ReleaseId = $_.ReleaseId
            $UBR = $_.UBR
            # Conditions
            if ($DisplayVersion.Length -le 0) { $WindowsVersion = $ReleaseId } else { $WindowsVersion = $DisplayVersion } # Greater Build 19042
            if (-not($Major.Length -le 0)) { $Major = "$Major." } # Add Points
            if (-not($Minor.Length -le 0)) { $Minor = "$Minor." } # Add Points
            if (-not($UBR.Length -le 0)) { $UBR = ".$UBR" } # Add Points
            if ($_ -match "Source OS") { $HistorySource = $true } else { $HistorySource = $false }
            # Object
            [PSCustomObject]@{
                PSTypeName     = "Get-WindowsVersion"
                ComputerName   = $env:COMPUTERNAME
                ProductName    = $ProductName
                WindowsVersion = $WindowsVersion
                WindowsBuild   = "$Major$Minor$Build$UBR"
                ProductId      = $ProductId
                InstallTime    = $InstallTime
                History        = $HistorySource
            }
        }
        if ($History) { $WindowsVersionObject } else { $WindowsVersionObject | Where-Object History -eq $false }
    }
    else {
        Write-Warning "PSVersion OS not Windows 10"
    }
}
New-Alias -Name gwv -Value Get-WindowsVersion
Export-ModuleMember -Alias * -Function Get-WindowsVersion