0
0
PowerShellscripting~10 mins

Invoke-Command in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Invoke-Command
Start Invoke-Command
Specify ScriptBlock or Command
Specify Target Computer(s)
Send Command to Target(s)
Execute Command Remotely
Receive Output
Display or Use Output
End
Invoke-Command sends a script or command to one or more remote computers, runs it there, and returns the output.
Execution Sample
PowerShell
Invoke-Command -ComputerName localhost -ScriptBlock { Get-Date }
Runs Get-Date on the local computer remotely and returns the current date and time.
Execution Table
StepActionTargetCommand SentOutput Received
1Start Invoke-CommandlocalhostN/AN/A
2Prepare ScriptBlocklocalhost{ Get-Date }N/A
3Send Command to localhostlocalhost{ Get-Date }N/A
4Execute Command Remotelylocalhost{ Get-Date }Current DateTime
5Receive OutputlocalhostN/ACurrent DateTime
6Display OutputlocalhostN/ACurrent DateTime
7End Invoke-CommandlocalhostN/ACurrent DateTime
💡 Command executed on localhost, output received and displayed.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
ScriptBlocknull{ Get-Date }{ Get-Date }{ Get-Date }
OutputnullnullCurrent DateTimeCurrent DateTime
Key Moments - 3 Insights
Why do we specify -ScriptBlock instead of just a command string?
Invoke-Command requires a ScriptBlock to run code remotely; this is shown in execution_table step 2 where the ScriptBlock { Get-Date } is prepared before sending.
What happens if the target computer is unreachable?
The command will fail to execute remotely and no output will be received, unlike step 5 where output is successfully received from localhost.
Can Invoke-Command run commands on multiple computers at once?
Yes, by specifying multiple computer names in -ComputerName, Invoke-Command sends the ScriptBlock to each target, similar to step 3 but repeated for each target.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output received at step 4?
AScriptBlock code
Bnull
CCurrent DateTime
DError message
💡 Hint
Check the 'Output Received' column at step 4 in the execution_table.
At which step is the ScriptBlock prepared before sending?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where ScriptBlock is prepared.
If the target computer is changed to multiple names, how does the execution_table change?
AMultiple rows for sending and receiving output for each computer
BOnly one row for all computers combined
CNo change in the table
DOutput column becomes empty
💡 Hint
Consider how Invoke-Command sends commands to each target separately as in step 3 and 5.
Concept Snapshot
Invoke-Command runs commands remotely.
Use -ComputerName to specify targets.
Use -ScriptBlock { } to specify commands.
Output returns from remote execution.
Supports multiple computers.
Useful for automation and management.
Full Transcript
Invoke-Command is a PowerShell command that lets you run scripts or commands on remote computers. You specify the computers with -ComputerName and the commands inside a ScriptBlock with -ScriptBlock. The command sends the script to the target, runs it there, and returns the output. This is useful for managing many computers at once or running tasks remotely. The execution flow starts by preparing the ScriptBlock, sending it to the target, executing it remotely, and then receiving and displaying the output. If the target is unreachable, no output is received. You can run commands on multiple computers by listing them in -ComputerName.