1

I need help to create a PowerShell script that will check for registry key only (not value) and will add registry key in case of absence of Registry key in the computer.

I've been able to add the tag using the script

 reg add "HKLM\SOFTWARE\WOW6432Node\Tanium\Tanium Client\Sensor Data\Tags" /v Test

But when trying to search the key using

Test-Path 'HKLM:\SOFTWARE\WOW6432Node\Tanium\Tanium Client\Sensor Data\Tags\Test'

It is showing False. No values need to be assigned to the key 'Test'. Just need a script that will return the value if the 'Test' tag has been created or not. If not, will be able to execute the script.

The below script is not capturing existence of the key 'Test'

$x =Get-ChildItem -Path 'HKLM:\SOFTWARE\WOW6432Node\Tanium\Tanium Client\Sensor Data\Tags' 
if($x -eq "Test") {
    write-host("Key is There")
}
Else {
    reg add "HKLM\SOFTWARE\WOW6432Node\Tanium\Tanium Client\Sensor Data\Tags" /v Test
}

Need help to get the correct checking criteria.

3
  • 2
    Please read about how to ask a good question. Commented Dec 23, 2021 at 12:16
  • 2
    What have you tried, and how has what you've tried failed? Ideally, you should provide a minimal reproducible example of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. Stack Overflow is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See How to Ask a Good Question (and How do I ask and answer homework questions, if relevant). Commented Dec 23, 2021 at 12:19
  • $x is not a string... Commented Dec 23, 2021 at 16:32

2 Answers 2

1

Test-Path can only check for key, not for it's properties.

For registry entries, key means the folder you can see using Registry Editor. Properties are the ones you can see on the right-hand side:

regedit screenshot showing key and property

To get the property you can use Get-ItemProperty cmdlet:

$regEntryPath = 'HKLM:\SOFTWARE\WOW6432Node\Tanium\Tanium Client\Sensor Data\Tags'

# Grab the property
$property = (Get-ItemProperty -Path $regEntryPath).Test
# Test if property exists
$null -ne $property

# Should return true

Let's also test whether the above works correctly for non-existing properties:

# Now check for non-existing property
$property2 = (Get-ItemProperty -Path $regEntryPath).NonExisting
$null -ne $property2

# Should return false
Sign up to request clarification or add additional context in comments.

1 Comment

It is returning True and Fales for existing and non-existing values respectively.
0

Use Test-Path instead of Get-ChildItem if you want to test if a registry key exists.

Also, better use New-Item to create the key if it does not exist yet instead of using reg.exe. Many group policies refuse the use of registry editing tools like reg.exe.

Try

$regPath = 'HKLM:\SOFTWARE\WOW6432Node\Tanium\Tanium Client\Sensor Data\Tags\Test'
if ((Test-Path -Path $regPath)) {
    Write-Host "Key exists"
}
else {
    New-Item -Path $regPath
}

Of course, doing this in the HKEY_LOCAL_MACHINE hive, you need to have admin permissions.

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.