Home > front end >  Make a registry change based on OS version
Make a registry change based on OS version

Time:09-21

I am trying to make a reg key change based on the OS version. The Key change pat works fine however the if function to work out if the device needs it or not I can not get to work. Any advice would be helpful. The powershell is below.

$verCheckOS = (Get-WmiObject win32_operatingsystem).version
    
if ($verCheckOS -lt 10.0.19043 -and $verCheckOS -gt 10.0.17134)
{
    if (Test-Path HKLM:\SOFTWARE\Policies\Microsoft\AzureADAccount)
    {
        CD HKLM:\SOFTWARE\Policies\Microsoft
        New-Item -Name AzureADAccount
        New-ItemProperty -Path "AzureADAccount" -Name "AllowPasswordReset" -Value 1 -PropertyType DWord
    }
}
Else
{
}

CodePudding user response:

To make PowerShell compare version numbers properly you need to cast them to the proper type.

$verCheckOS = [version](Get-CimInstance -ClassName CIM_OperatingSystem).Version 
if ($verCheckOS -lt [version]'10.0.19043' -and $verCheckOS -gt [version]'10.0.17134') {
    if (-not (Test-Path HKLM:\SOFTWARE\Policies\Microsoft\AzureADAccount)) {
        Push-Location 'HKLM:\SOFTWARE\Policies\Microsoft'
        New-Item -Name 'AzureADAccount' 
        New-ItemProperty -Path 'AzureADAccount' -Name 'AllowPasswordReset' -Value 1 -PropertyType DWord
    }
}

CodePudding user response:

$verCheckOS = (Get-WmiObject win32_operatingsystem).version        
if ($verCheckOS -lt "10.0.19043" -and $verCheckOS -gt "10.0.17134")
{
    if (!(Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\AzureADAccount"))
    {
        $null = New-Item -Name "AzureADAccount" -Path "HKLM:\SOFTWARE\Policies\Microsoft\"
        $null = New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\AzureADAccount" -Name "AllowPasswordReset" -Value 1 -PropertyType DWord
    }
}

I am a bit confused by your code. You check if the full path to the AzureADAccount key exists and then if it does you proceed to try and create it? I think you meant if it does NOT exist then create it? I mean your code won't execute if it isn't there so the line to create it is just going to error out. So I changed it to run the block only if the path doesn't exist. If that's wrong then put the Test-Path line back the way you had it and remove the 'New-Item -Name "AzureADAccount"' line as its meaningless and keep the New-ItemProperty line.

Also just adding quotes around the numbers made the version check if statement work fine for me in my testing.

  • Related