November 7th, 2012

PowerTip: Run a PowerShell Command on Multiple Servers at the Same Time

Doctor Scripto
Scripter

Summary:  Run a Windows PowerShell cmdlet on multiple servers at the same time.

Hey, Scripting Guy! Question How can I run a Windows PowerShell cmdlet on multiple servers at the same time?

              Hey, Scripting Guy! Answer To run a Windows PowerShell cmdlet on multiple servers at the same time use the New-PSSession cmdlet and save the returned session object in a variable. Now use the Invoke-Command cmdlet with the session object to run the command. Use the ScriptBlock parameter to specify the cmdlet to run.

$session = New-PSSession -cn dc2,dc3 -cred iammred\administrator

Invoke-Command -Session $session -ScriptBlock {get-date}

 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.