Running Commands On Multiple Computers

by Nov 24, 2009

You can work remotely not just for single computers. You can also run your commands against multiple computers like this:

$computer = Get-Content computers.txt
$session = New-PSSession -ComputerName $computer
Invoke-Command -Session $session { md hklm:\software\hey }

Here, you can see how remote commands reuse an existing session that was created previously with New-PSSession. You can specify one or more computer names when you establish the session. In the example, all computer names are read from a plain text file.

The command in brackets is simultaneously executed on all computers the session was set up for whenever you use Invoke-Command against the existing session. In the example, a new registry key is created. PowerShell limits the number of simultaneous connections to 32 (unless you change this with the -throttleLimit parameter) so if you should specify more than 32 computers, it will postpone additional calls.

Twitter This Tip! ReTweet this Tip!