liveBook · Manning

Invoke-Command

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Invoke-Command is a PowerShell cmdlet that runs commands or script blocks on local or remote computers.

Invoke-Command runs a command or script block on one or more computers. Its remoting support lets you administer remote hosts without installing every administrative cmdlet locally. section 13.0

Use Invoke-Command to:

  • Run commands on several computers at the same time; PowerShell executes the script block independently on each host and returns the results. section 13.6
  • Supply computer names directly or read them from a file with Get-Content. section 13.11
  • Use an existing session to avoid repeatedly establishing remote connections. section 18.6

Retrieve the newest files remotely

Invoke-Command -ScriptBlock { Get-ChildItem ~/* | Sort-Object LastWriteTime -Descending | Select-Object -First 10 } -HostName (Get-Content computers.txt) -UserName yourUser The commands inside the braces run on each target computer. Keep filtering, sorting, and other actions that must affect remote objects inside the script block. Format or export the returned results locally. section 13.11 section 13.7.2

Invoke-Command -ComputerName SRV2,DC3,SRV4
-ScriptBlock { Get-Process pwsh } -UserName tylerl

Compare a one-to-many command using ComputerName, a script block, and credentials. section 13.6

Remote results are serialized before returning and may no longer behave like live remote objects. Therefore, place actions such as stopping, starting, or changing processes inside the remote script block; use local processing mainly for display, reports, or data files. section 13.7.2

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains targeting, script blocks, concurrency, sessions, serialization, and remote action placement through runnable examples and labs.
Examples
Demonstrates process retrieval, top-process queries, newest-file lists, version checks, event logs, and session-based commands.
What it assumes
Assumes basic PowerShell commands and pipelines while teaching remoting, sessions, and remote execution through guided labs.
What you can do after
Operate/configure remote administration tasks across hosts and diagnose local-versus-remote processing mistakes.
How it connects
Connects remoting with script blocks, PowerShell sessions, serialization, background jobs, and formatting or export pipelines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.