PowerShell - Active DirectoryYou want to check if the AD module is installed and import it only if present. Which script snippet is correct?AImport-Module ActiveDirectory -ErrorAction SilentlyContinueBif (Get-Module -ListAvailable -Name ActiveDirectory) { Import-Module ActiveDirectory }CGet-WindowsFeature RSAT-AD-PowerShell | Import-ModuleDAdd-WindowsCapability -Online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0Check Answer
Step-by-Step SolutionSolution:Step 1: Check module availabilityGet-Module -ListAvailable checks if the module is installed on the system.Step 2: Import module conditionallyIf the module is found, Import-Module loads it into the session.Step 3: Verify other optionsImport-Module ActiveDirectory -ErrorAction SilentlyContinue imports without checking; C is invalid syntax; D installs module but does not check.Final Answer:if (Get-Module -ListAvailable -Name ActiveDirectory) { Import-Module ActiveDirectory } -> Option BQuick Check:Check then import module = if (Get-Module -ListAvailable -Name ActiveDirectory) { Import-Module ActiveDirectory } [OK]Quick Trick: Use Get-Module -ListAvailable to check before importing [OK]Common Mistakes:Importing without checking causes errorsUsing invalid pipeline commandsConfusing install commands with import
Master "Active Directory" in PowerShell9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PowerShell Quizzes Active Directory - Organizational unit operations - Quiz 7medium Automation Patterns - Monitoring scripts with email alerts - Quiz 10hard Automation Patterns - Report generation automation - Quiz 6medium Automation Patterns - Configuration drift detection - Quiz 15hard Automation Patterns - Desired State Configuration (DSC) basics - Quiz 12easy Cross-Platform PowerShell - Why cross-platform extends reach - Quiz 3easy Cross-Platform PowerShell - PowerShell on macOS - Quiz 4medium Remote Management - PSSession management - Quiz 8hard Scripting Best Practices - Code signing - Quiz 7medium System Administration - Service management (Get/Start/Stop-Service) - Quiz 2easy