Check for matching registry values.

Welcome Forums General PowerShell Q&A Check for matching registry values.

Viewing 7 reply threads
  • Author
    Posts
    • #218979
      Participant
      Topics: 1
      Replies: 2
      Points: -5
      Rank: Member

      Hello,
      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}

       

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

       

      • This topic was modified 1 month, 2 weeks ago by KaptKirk87. Reason: Formating
    • #218988
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Shashi, welcome to Powershell.org. 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.

    • #218994
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

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

      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. 😉

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

      Get-Help about_switch
    • #219009
      Participant
      Topics: 1
      Replies: 2
      Points: -5
      Rank: Member

      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.

    • #219018
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      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.

    • #219057
      Participant
      Topics: 1
      Replies: 2
      Points: -5
      Rank: Member

      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.

    • #219108
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      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.

    • #219192
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      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.

      Powershell vs. Registry … it might not be the best topic to start with. 😉 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.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.