Bird
Raised Fist0
PowerShellscripting~20 mins

Invoke-Command in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Invoke-Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Invoke-Command script?
Consider this PowerShell script that runs a command on a remote computer named 'Server01'. What will be the output?
PowerShell
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Date -Format 'yyyy-MM-dd' }
AThe current date on Server01 in 'yyyy-MM-dd' format, e.g., '2024-06-15'
BThe current date on the local computer in 'yyyy-MM-dd' format
CAn error saying 'ComputerName Server01 not found'
DNo output, the command runs silently
Attempts:
2 left
💡 Hint
Invoke-Command runs the script block on the remote computer specified by -ComputerName.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses Invoke-Command to run a script block on multiple computers?
You want to run the command 'hostname' on two remote computers: 'PC1' and 'PC2'. Which syntax is correct?
AInvoke-Command -ComputerName PC1, PC2 -ScriptBlock { hostname }
BInvoke-Command -ComputerName 'PC1 PC2' -ScriptBlock { hostname }
CInvoke-Command -ComputerName @('PC1','PC2') -ScriptBlock { hostname }
DInvoke-Command -ComputerName ['PC1','PC2'] -ScriptBlock { hostname }
Attempts:
2 left
💡 Hint
The -ComputerName parameter accepts a comma-separated list of computer names.
🔧 Debug
advanced
2:00remaining
Why does this Invoke-Command script fail with an error?
This script tries to run a command on a remote computer but fails. What is the cause?
PowerShell
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process } -Credential $null
AThe script block is invalid because Get-Process cannot run remotely.
BPassing $null to -Credential causes an error because a valid credential is required for remote sessions.
CThe ComputerName parameter is missing quotes around Server01.
DInvoke-Command does not support the -Credential parameter.
Attempts:
2 left
💡 Hint
Check the use of the -Credential parameter and what values it accepts.
🚀 Application
advanced
2:00remaining
How to capture output from multiple remote computers using Invoke-Command?
You want to run 'Get-Service' on 'ServerA' and 'ServerB' and save the combined output locally. Which script achieves this?
A$results = Invoke-Command -ComputerName ServerA, ServerB -ScriptBlock { Get-Service }; $results
BInvoke-Command -ComputerName ServerA, ServerB -ScriptBlock { Get-Service } | Out-File services.txt
C$results = Invoke-Command -ComputerName ServerA -ScriptBlock { Get-Service }; $results += Invoke-Command -ComputerName ServerB -ScriptBlock { Get-Service }; $results
DInvoke-Command -ComputerName ServerA, ServerB -ScriptBlock { Get-Service } > services.txt
Attempts:
2 left
💡 Hint
Invoke-Command returns output objects that can be stored in a variable.
🧠 Conceptual
expert
2:00remaining
What happens if you use Invoke-Command with -AsJob parameter?
You run this command: Invoke-Command -ComputerName Server01 -ScriptBlock { Start-Sleep -Seconds 10; 'Done' } -AsJob. What is the behavior?
AThe command runs asynchronously but outputs 'Done' immediately.
BThe command runs synchronously and outputs 'Done' after 10 seconds.
CThe command fails because -AsJob cannot be used with Invoke-Command.
DThe command runs asynchronously as a background job; you must use Receive-Job to get the output later.
Attempts:
2 left
💡 Hint
The -AsJob parameter runs the command in the background.

Practice

(1/5)
1. What is the primary purpose of the Invoke-Command cmdlet in PowerShell?
easy
A. To display system event logs
B. To run commands on remote or local computers
C. To edit files locally
D. To create new user accounts

Solution

  1. Step 1: Understand the cmdlet's function

    Invoke-Command is designed to run commands remotely or locally on one or more computers.
  2. Step 2: Compare options

    Options A, B, and C describe unrelated tasks like user management, file editing, or log viewing, which are not the main purpose of Invoke-Command.
  3. Final Answer:

    To run commands on remote or local computers -> Option B
  4. Quick Check:

    Invoke-Command runs commands remotely or locally [OK]
Hint: Invoke-Command runs scripts on other computers [OK]
Common Mistakes:
  • Confusing Invoke-Command with file editing cmdlets
  • Thinking it only works locally
  • Assuming it manages user accounts
2. Which of the following is the correct syntax to run a script block on a remote computer named 'Server01' using Invoke-Command?
easy
A. Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }
B. Invoke-Command Server01 -ScriptBlock Get-Process
C. Invoke-Command -ScriptBlock Server01 { Get-Process }
D. Invoke-Command -ComputerName { Server01 } -ScriptBlock Get-Process

Solution

  1. Step 1: Identify correct parameter usage

    The -ComputerName parameter expects the remote computer's name, and -ScriptBlock expects a script block enclosed in braces.
  2. Step 2: Validate syntax correctness

    Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process } correctly uses -ComputerName Server01 and -ScriptBlock { Get-Process }. Other options misuse parameters or omit braces.
  3. Final Answer:

    Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process } -> Option A
  4. Quick Check:

    Correct syntax uses -ComputerName and script block braces [OK]
Hint: Use braces {} for script block and -ComputerName for target [OK]
Common Mistakes:
  • Omitting braces around script block
  • Placing computer name inside braces
  • Using script block without braces
3. What will be the output of this command?
Invoke-Command -ComputerName localhost -ScriptBlock { 2 + 3 }
medium
A. Error: Cannot find computer
B. 2 + 3
C. 5
D. null

Solution

  1. Step 1: Understand command execution on localhost

    The command runs the script block { 2 + 3 } on the local computer named 'localhost'.
  2. Step 2: Calculate the script block result

    The expression 2 + 3 evaluates to 5, so the output will be 5.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    2 + 3 = 5 [OK]
Hint: Script block runs and returns result, not expression text [OK]
Common Mistakes:
  • Expecting the expression as string output
  • Assuming remote computer error on localhost
  • Thinking output is null
4. You run this command:
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Service }

But get an error: "Access is denied." What is the most likely cause?
medium
A. The script block syntax is incorrect
B. Get-Service cmdlet does not exist
C. Server01 is offline
D. You do not have permission to run commands on Server01

Solution

  1. Step 1: Analyze the error message

    "Access is denied" indicates a permissions issue, not syntax or cmdlet existence.
  2. Step 2: Match error to cause

    Permission problems usually mean the user lacks rights to run remote commands on Server01.
  3. Final Answer:

    You do not have permission to run commands on Server01 -> Option D
  4. Quick Check:

    Access denied = permission issue [OK]
Hint: Access denied usually means permission problem [OK]
Common Mistakes:
  • Assuming syntax error causes access denied
  • Thinking server offline causes access denied
  • Believing cmdlet absence causes access denied
5. You want to run a command on multiple remote computers: Server01, Server02, and Server03. Which command correctly runs Get-EventLog -LogName System -Newest 5 on all three at once?
hard
A. Invoke-Command -ComputerName @('Server01', 'Server02', 'Server03') -ScriptBlock { Get-EventLog -LogName System -Newest 5 }
B. Invoke-Command -ComputerName 'Server01 Server02 Server03' -ScriptBlock { Get-EventLog -LogName System -Newest 5 }
C. Invoke-Command -ComputerName Server01 Server02 Server03 -ScriptBlock { Get-EventLog -LogName System -Newest 5 }
D. Invoke-Command -ComputerName Server01; Server02; Server03 -ScriptBlock { Get-EventLog -LogName System -Newest 5 }

Solution

  1. Step 1: Understand how to specify multiple computers

    The -ComputerName parameter accepts an array of strings to target multiple computers.
  2. Step 2: Evaluate options for correct array syntax

    Invoke-Command -ComputerName @('Server01', 'Server02', 'Server03') -ScriptBlock { Get-EventLog -LogName System -Newest 5 } uses an array @('Server01', 'Server02', 'Server03'), which is the correct way to pass multiple names. Invoke-Command -ComputerName Server01 Server02 Server03 -ScriptBlock { Get-EventLog -LogName System -Newest 5 } uses space-separated names without commas or array syntax, which is invalid. Invoke-Command -ComputerName 'Server01 Server02 Server03' -ScriptBlock { Get-EventLog -LogName System -Newest 5 } passes a single string with spaces, treated as one name. Invoke-Command -ComputerName Server01; Server02; Server03 -ScriptBlock { Get-EventLog -LogName System -Newest 5 } uses semicolons, which is invalid syntax.
  3. Final Answer:

    Invoke-Command -ComputerName @('Server01', 'Server02', 'Server03') -ScriptBlock { Get-EventLog -LogName System -Newest 5 } -> Option A
  4. Quick Check:

    Use array syntax @() for multiple computers [OK]
Hint: Use @() array for multiple computer names [OK]
Common Mistakes:
  • Using space-separated names without commas or array syntax
  • Passing all names as one string
  • Using semicolons to separate names