A technician needs to deploy a configuration change to 50 Windows 10 computers using a script. The script must check if a specific registry key exists before modifying it. Which scripting construct should be used?
An if-else statement is the most appropriate control structure for this scenario because it explicitly allows a script to evaluate a specific condition, such as the existence of a registry key. If the condition evaluates to true (e.g., the key exists), a defined block of code is executed. Optionally, an 'else' block can be executed if the condition is false, providing a clear path for conditional logic and ensuring the configuration change is applied only when necessary or handled differently if the key is absent.
Why this answer
The script needs to conditionally execute code based on whether a registry key exists. An if-else statement is the correct construct for this because it evaluates a condition (e.g., Test-Path 'HKLM:\Software\MyKey') and executes one block if true (modify the key) and another if false (skip or create). Loops are for repetition, not conditional branching, and try-catch handles runtime errors, not existence checks.
Exam trap
The trap here is that candidates confuse conditional logic (if-else) with error handling (try-catch), thinking that checking for existence requires exception handling, when in fact a simple conditional test is the correct and more efficient approach.
How to eliminate wrong answers
Option A is wrong because a for loop is designed for iterating over a sequence or a fixed number of times, not for making a single conditional decision about a registry key's existence. Option B is wrong because a while loop repeats a block of code as long as a condition is true, which is unnecessary for a one-time check and could cause an infinite loop if misused. Option D is wrong because a try-catch block is used to handle exceptions (runtime errors) such as access denied or missing paths, not to test for the existence of a registry key before modification.