#PowerShell script to Compare a Registry value string.

Here is a #PowerShell script to Compare a Registry version string to the version string passed as a parameter switch. And, it comes with logging as well and that too in CMTRACE format.

The logging snippet has been borrowed from Russ Slaten and the script is available for download here.

Example: To Compare the Notepad++ version from the Software – Uninstall Registry Hive

.\CompareRegVersion.v1.ps1 -TargetVersion "9.20.00.0" -RegRoot "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{23170F69-40C1-2702-0920-000001000000}" -RegValue "DisplayVersion"

When the Installed version is equal to Target version

VersionEqual

When the Installed version is higher than the Target version

InstalledVersionHigher

When the Installed version is smaller that the Target version

InstalledversionLower

The logs are logged to “$env:Windir\Debug\RegCompareVer.log” as shown below

CompareLogging

Here is the full script

  <#
    .SYNOPSIS
        Compares a registry version string to the version passed to the script via commandline.

    .DESCRIPTION
        The script compares the registry version string "DisplayVersion" (typically from ADD/Remove programs) to a version string that is passed to the script via the parameter -targetVersion.
        Both strings are converted to version objects to ensure a reliable comparison.

        Version strings containing spaces e.g. "1.1.0.0 R4" are stripped to Microsoft's standard notation "Major.Minor.Build.Revision"

        Return values:
         0: InstalledVersion = TargetVersion
         1: InstalledVersion > TargetVersion
         2: InstalledVersion < TargetVersion
        99: Registry hive not found

    .EXAMPLE
        To Compare the Notepad++ version from the Software - Uninstall Registry Hive

.\CompareRegVersion.v1.ps1 -TargetVersion "10.20.00.0" -RegRoot "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{23170F69-40C1-2702-0920-000001000000}" -RegValue "DisplayVersion"

  #>

param(

[Parameter(Mandatory=$True)][ValidateNotNull()][Version]$TargetVersion,
[Parameter(Mandatory=$True)][ValidateNotNull()][string]$RegRoot,
[Parameter(Mandatory=$True)][ValidateNotNull()][string]$RegValue

)

###############################################################################

Function LogIt
{
  Param (
  [Parameter(Mandatory=$True)]
  $Message,
  [Parameter(Mandatory=$True)]
  $Component,
  [Parameter(Mandatory=$True)]
  $Type )

  switch ($Type)
  {
    1 { $Type = "Info" }
    2 { $Type = "Warning" }
    3 { $Type = "Error" }
    4 { $Type = "Verbose" }
  }

  if (($Type -eq "Verbose") -and ($Global:Verbose))
  {
    $ToLog = "{0} `$$<{1}><{2} {3}><thread={4}>" -f ($Type + ":" + $Message), ($Global:ScriptName + " : " + $Component), (Get-Date -Format "MM-dd-yyyy"), (Get-Date -Format "HH:mm:ss.ffffff"), $pid
    $ToLog | Out-File -Append -Encoding UTF8 -FilePath ("filesystem::{0}" -f $Global:LogFile)
    Write-Host $Message
  }
  elseif ($Type -ne "Verbose")
  {
    $ToLog = "{0} `$$<{1}><{2} {3}><thread={4}>" -f ($Type + ":" + $Message), ($Global:ScriptName + " : " + $Component), (Get-Date -Format "MM-dd-yyyy"), (Get-Date -Format "HH:mm:ss.ffffff"), $pid
    $ToLog | Out-File -Append -Encoding UTF8 -FilePath ("filesystem::{0}" -f $Global:LogFile)
    Write-Host $Message
  }
  if (($Type -eq 'Warning') -and ($Global:ScriptStatus -ne 'Error')) { $Global:ScriptStatus = $Type }
  if ($Type -eq 'Error') { $Global:ScriptStatus = $Type }

  if ((Get-Item $Global:LogFile).Length/1KB -gt $Global:MaxLogSizeInKB)
  {
    $log = $Global:LogFile
    Remove-Item ($log.Replace(".log", ".lo_"))
    Rename-Item $Global:LogFile ($log.Replace(".log", ".lo_")) -Force
  }
}

$VerboseLogging = "true"
[bool]$Global:Verbose = [System.Convert]::ToBoolean($VerboseLogging)
$Global:LogFile = "$env:Windir\Debug\RegCompareVer.log"
$Global:MaxLogSizeInKB = 10240
$Global:ScriptName = 'LogIt.ps1' 
$Global:ScriptStatus = 'Success'

###############################################################################

LogIt -message (" ################################### Reg Version Compare ###################################") -component "CompareRegVersion" -type 1
LogIt -message (" Parameters: TargetVersion = $TargetVersion RegRoot = $RegRoot RegValue = $RegValue") -component "CompareRegVersion" -type 1

Try
{

    $InstalledVersion = (Get-ItemProperty -Path $RegRoot -ErrorAction Stop).$RegValue.ToString()

    if ($InstalledVersion -match ' ')
    {
        $v0 = [Version]($InstalledVersion.Split(" "))[0]
        $v0Type = "String"
    }
    else
    {
        $v0 = [Version] $InstalledVersion
        $v0Type = "Version"
    }

    if ($TargetVersion -match ' ')
    {
        $v1 = [Version]($TargetVersion.Split(" "))[0]
        $v1Type = "String"
    }
    else
    {
        $v1 = [Version] $TargetVersion
        $v1Type = "Version"
    }

    if ($v0 -lt $v1)
    {
        LogIt -message (" Installed version ($v0) smaller than Target version ($v1), exiting with 2") -component "CompareRegVersion" -type 1
        LogIt -message (" ################################### Exit Reg Version Compare ###################################") -component "CompareRegVersion" -type 1
           
        exit 2
    }
    elseif ($v0 -eq $v1)
    {
        LogIt -message (" Installed and Target versions are equal ($v0 = $v1), exiting with 0") -component "CompareRegVersion" -type 1
        LogIt -message (" ################################### Exit Reg Version Compare ###################################") -component "CompareRegVersion" -type 1
        exit 0
    }
    elseif ($v0 -gt $v1)
    {
        LogIt -message (" Installed version ($v0) greater than Target version ($v1), exiting with 1") -component "CompareRegVersion" -type 1
        LogIt -message (" ################################### Exit Reg Version Compare ###################################") -component "CompareRegVersion" -type 1
        exit 1
    }
}

Catch
{
    Write-Output "Error occurred, RegValue or Registry Hive not found. Exiting with 99"
    LogIt -message (" Error occurred, RegValue or Registry Hive not found. Exiting with 99") -component "CompareRegVersion" -type 3
    LogIt -message (" ################################### Exit Reg Version Compare ###################################") -component "CompareRegVersion" -type 3  
    exit 99
}

Thanks once again. Hope this helps.

1 thought on “#PowerShell script to Compare a Registry value string.

  1. You should take part in a contest for the most effective blogs on the web. I’ll suggest this web site!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: