0

I have the following code that I need to use to switch windows 7 themes between standard and high contrast. The IF runs fine but it will not run the ELSE condition, can someone please point out the error of my ways?

IF     ((Get-ItemProperty -path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes").CurrentTheme = "%SystemRoot%\resources\Ease of Access Themes\hcblack.theme") 
       {
       C:\scripts\Themetool.exe changetheme "c:\Windows\resources\Themes\MyTheme.theme"
       } 
ELSE   {
       C:\scripts\Themetool.exe changetheme "C:\Windows\resources\Ease of Access Themes\hcblack.theme"
       }

1 Answer 1

5

You are assigning the current theme in the registry.

You need to use -eq for equality comparison. = is the assign operator in powershell.

List of powershell operators

What happens in detail is that your code is first assigning the value .../hcblack.theme to CurrentTheme and then uses this value for the boolean condition in the if statement. PowerShell treats non empty strings as $true. You can try this yourself: !!"" -eq $false. That's why the if part is matched.

What you are doing could be written as:

$prop = Get-ItemProperty -path HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes"
$prop.CurrentTheme = "%SystemRoot%\resources\Ease of Access Themes\hcblack.theme"
if ($prop.CurrentTheme) { ... }

What you should do:

if ((Get-ItemProperty -path "<path>").CurrentTheme -eq "<value>") { ... } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.