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
