Bird
0
0

You want to create a new registry key HKCU:\Software\MyApp\Settings only if it does not exist. Which script snippet correctly does this?

hard📝 Application Q15 of 15
PowerShell - System Administration
You want to create a new registry key HKCU:\Software\MyApp\Settings only if it does not exist. Which script snippet correctly does this?
Aif (-not (Test-Path 'HKCU:\Software\MyApp\Settings')) { New-Item -Path 'HKCU:\Software\MyApp' -Name 'Settings' }
BNew-Item -Path 'HKCU:\Software\MyApp\Settings' -Force
CSet-ItemProperty -Path 'HKCU:\Software\MyApp\Settings' -Name 'Exists' -Value $true
DRemove-Item -Path 'HKCU:\Software\MyApp\Settings' -ErrorAction SilentlyContinue
Step-by-Step Solution
Solution:
  1. Step 1: Check if key exists using Test-Path

    Test-Path returns true if the registry key exists, so -not negates it to check non-existence.
  2. Step 2: Create key only if missing

    New-Item creates the 'Settings' key under 'MyApp' only if it does not exist, avoiding errors.
  3. Final Answer:

    if (-not (Test-Path 'HKCU:\Software\MyApp\Settings')) { New-Item -Path 'HKCU:\Software\MyApp' -Name 'Settings' } -> Option A
  4. Quick Check:

    Use Test-Path before New-Item to avoid duplicates [OK]
Quick Trick: Use Test-Path to check key before creating with New-Item [OK]
Common Mistakes:
  • Using New-Item with -Force creates or overwrites without check
  • Using Set-ItemProperty to create keys (it sets values)
  • Deleting key instead of creating it

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes