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
Using Invoke-Command to Run Remote Commands
📖 Scenario: You are managing multiple computers in your office network. You want to run a simple command on a remote computer to check its system information without logging in physically.
🎯 Goal: Learn how to use Invoke-Command in PowerShell to run a command on a remote computer and get the output.
📋 What You'll Learn
Create a variable with the remote computer name
Create a script block with the command to run remotely
Use Invoke-Command to run the script block on the remote computer
Display the output of the remote command
💡 Why This Matters
🌍 Real World
System administrators often need to run commands on remote computers to gather information or perform tasks without physically accessing each machine.
💼 Career
Knowing how to use Invoke-Command is essential for IT professionals managing Windows networks and automating remote tasks.
Progress0 / 4 steps
1
Set the remote computer name
Create a variable called $computerName and set it to the string "Server01".
PowerShell
Hint
Use = to assign the string "Server01" to the variable $computerName.
2
Create the script block with the command
Create a variable called $scriptBlock and assign it a script block that runs the command Get-ComputerInfo.
PowerShell
Hint
Use curly braces { } to create a script block and put Get-ComputerInfo inside.
3
Run Invoke-Command on the remote computer
Use Invoke-Command with the -ComputerName parameter set to $computerName and the -ScriptBlock parameter set to $scriptBlock. Store the result in a variable called $result.
PowerShell
Hint
Use Invoke-Command with the parameters exactly as shown and assign the output to $result.
4
Display the output of the remote command
Print the contents of the variable $result to show the remote computer information.
PowerShell
Hint
Simply type $result on a line by itself to display the output.
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
Step 1: Understand the cmdlet's function
Invoke-Command is designed to run commands remotely or locally on one or more computers.
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.
Final Answer:
To run commands on remote or local computers -> Option B
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
Step 1: Identify correct parameter usage
The -ComputerName parameter expects the remote computer's name, and -ScriptBlock expects a script block enclosed in braces.
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.
Final Answer:
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process } -> Option A
Quick Check:
Correct syntax uses -ComputerName and script block braces [OK]
Hint: Use braces {} for script block and -ComputerName for target [OK]
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
Step 1: Analyze the error message
"Access is denied" indicates a permissions issue, not syntax or cmdlet existence.
Step 2: Match error to cause
Permission problems usually mean the user lacks rights to run remote commands on Server01.
Final Answer:
You do not have permission to run commands on Server01 -> Option D
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
Step 1: Understand how to specify multiple computers
The -ComputerName parameter accepts an array of strings to target multiple computers.
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.
Final Answer:
Invoke-Command -ComputerName @('Server01', 'Server02', 'Server03') -ScriptBlock { Get-EventLog -LogName System -Newest 5 } -> Option A
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