Bird
0
0

You want to create a script that checks if a registry key HKCU:\Software\MyApp exists, and if not, creates it. Which snippet correctly does this?

hard📝 Application Q8 of 15
PowerShell - System Administration
You want to create a script that checks if a registry key HKCU:\Software\MyApp exists, and if not, creates it. Which snippet correctly does this?
ASet-ItemProperty -Path 'HKCU:\Software\MyApp' -Name 'Exists' -Value $true
Bif (-not (Test-Path 'HKCU:\Software\MyApp')) { New-Item -Path 'HKCU:\Software' -Name 'MyApp' }
CNew-Item -Path 'HKCU:\Software\MyApp' -Force
Dif (Test-Path 'HKCU:\Software\MyApp') { Remove-Item -Path 'HKCU:\Software\MyApp' }
Step-by-Step Solution
Solution:
  1. Step 1: Check key existence

    Test-Path returns true if the key exists; -not negates it.
  2. Step 2: Create key if missing

    New-Item creates 'MyApp' under 'HKCU:\Software' only if missing.
  3. Final Answer:

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

    Use Test-Path and New-Item to conditionally create keys [OK]
Quick Trick: Use Test-Path to check key before creating [OK]
Common Mistakes:
  • Deleting key if exists instead of creating
  • Using New-Item without checking existence
  • Trying to set a property instead of creating key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes