Check for matching registry values.

I’ve decided to invest some time in learning how to use Powershell. My current knowledge is limited, but I am always willing to learn.

I was wondering if someone could help me create a PowerShell script. I’ve spent several hours already on it, and because of my limited knowledge, I keep hitting roadblocks.

The script should be simple.

I am checking to see if a registry value for a value equals false, then change it to 1, if it is 1, change it to 2, if 2, change it to 3, if 3, change it back to 0.

$registryPath = "HKLM:\SOFTWARE\Temp\"

$Name = “Cat”

$data = “0”

IF(!(Test-Path $registryPath -Value $Name - $data))


New-Item -Path $registryPath -Force | Out-Null

New-ItemProperty -Path $registryPath -Name $name -Value $value `</div>

<div> -PropertyType DWORD -Force | Out-Null}</div>
<div> ELSE {</div>
<div> New-ItemProperty -Path $registryPath -Name $name -Value $value `

-PropertyType DWORD -Force | Out-Null}</pre>

Powershell tells me the parameter cannot be found for “Name”


Shashi, welcome to Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

Please go back and fix your post by formatting your code as code using the code tags “PRE”, please. You should format error messages or sample data or console output as code as well. Thanks in advance.

Hmmm … still not really good but I accept your attempt. :wink:

You could start like this:

$registryPath = ‘REGISTRY::HKLM\SOFTWARE\Temp’
$Name = ‘Cat’
$value = 0
IF (-not (Test-Path $registryPath)) {
New-Item -Path $registryPath -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force | Out-Null

… now you try to create your check and change cascade by yourself. :wink:

You may want to read the help for the switch statement:

Get-Help about_switch

How do you have PS verify if the “Value data” is correct in line 4? It is only checking for the path, which is fine.

Test-Path does not check for a particular value it only checks for existance of a path. That’s what it’s made for. It checks if the registry key exists and if not it creates it. Then the second command creates the registry value with it’s value. You might search for additional information about the registry provider and how to work with it.

That is what I will need help in verifying - if the “Data value” is there, then enter the new data value. Maybe that is not the best way to handle it. If it is not possible, then I am okay with it.

Oh it’s 100% possible. Olaf recommended searching for and how to work with the powershell registry PSprovider. Maybe you can figure it out, maybe you can’t. Come back and post what you’ve tried and where it’s failing you, and I know someone here will be willing to help you figure it out. I do not think anyone will just do it for you, however.

Powershell vs. Registry … it might not be the best topic to start with. :wink: The registry provider and its mechanisms may be a little hard to understand for beginners sometimes. You cannot use Test-Path to check a registry entry and its value - that works only for registry keys. Think of them as they are like directories in your file system. But unfortunately registry entries are not comparable to files. You might start with reading the help topics about Working with Registry Keys and Working with Registry Entries. To check a registry key’s entry and its value you need to use Get-ItemProperty.

Please always read the complete help including the examples for all cmdlets you’re about to use to learn how to use them.