Registry Check and Fix Script in PowerShell

In this blog post, we'll be exploring a PowerShell script that checks the registry for specific settings and makes adjustments as needed. The script, Fix-services.ps1, can be found on GitHub.

Prerequisites


The Script

The script starts by defining a registry path:

$regpath = 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\MUI\StringCacheSettings'
if (Test-path $regpath -ErrorAction SilentlyContinue) {
        Write-output "$regpath exists :)"
    }
    else {
        Write-output "$regpath doesn't exist :("
        new-item -Path HKLM:\SYSTEM\CurrentControlSet\Control\MUI\StringCacheSettings -Name StringCacheSettings -ItemType Directory -Force
    }

How It Works

Checking the Registry Path

The script first checks if the registry path exists using the `Test-path` cmdlet. If it does, the script outputs a message indicating that the path exists.

Making Adjustments

If the registry path doesn't exist, the script creates a new directory with the name "StringCacheSettings" at the specified location using the `New-item` cmdlet.

Key Code Snippets

$value = $null
$value = get-itempropertyvalue -Path $regpath -name StringCacheGeneration -ErrorAction SilentlyContinue    
if ($value -eq '907') {
    Write-output "`tregistry Values good"
    $good++
}
else {
    Write-output "`tRegistry Value does not exist or is bad"
    new-itemproperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\MUI\StringCacheSettings" -PropertyType Dword -Name StringCacheGeneration -Value "907" -force
    $bad++
    write-warning "Please restart $env:computername"
}

Usage Examples

This script can be used to check and adjust the registry settings for StringCacheGeneration. The script will output messages indicating whether the path exists, and if not, whether it was created successfully.

Conclusion

In this blog post, we've explored a PowerShell script that checks the registry for specific settings and makes adjustments as needed. The script is designed to be flexible and adaptable to different scenarios, making it a valuable tool in any IT professional's toolkit.