Bird
0
0

You want to import a module only if it is not already loaded in your PowerShell session. Which script snippet correctly does this?

hard📝 Application Q15 of 15
PowerShell - Modules and Script Organization
You want to import a module only if it is not already loaded in your PowerShell session. Which script snippet correctly does this?
AImport-Module MyModule -Force
Bif (-not (Get-Module -Name MyModule)) { Import-Module MyModule }
CGet-Module MyModule | Import-Module
DImport-Module MyModule -SkipIfLoaded
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to check if a module is loaded

    Get-Module -Name MyModule returns the module if loaded, or nothing if not.
  2. Step 2: Use conditional to import only if not loaded

    The condition -not (Get-Module -Name MyModule) checks if module is missing, then imports it.
  3. Step 3: Evaluate other options

    Import-Module MyModule -Force forces import regardless. Piping Get-Module output to Import-Module is invalid syntax. -SkipIfLoaded is not a valid parameter.
  4. Final Answer:

    if (-not (Get-Module -Name MyModule)) { Import-Module MyModule } -> Option B
  5. Quick Check:

    Check with Get-Module before Import-Module [OK]
Quick Trick: Check Get-Module before Import-Module to avoid duplicates [OK]
Common Mistakes:
  • Using Import-Module -Force to avoid checking
  • Trying to pipe Get-Module output to Import-Module
  • Using non-existent parameters like -SkipIfLoaded

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes