3. Run Powershell Invoke-Command
Once the script is on the local computer, specify the computers it should run on and the path where it’s stored. To do this, run Invoke-Command, providing computer names to the ComputerName parameter and the path to the script to the FilePath parameter like below:
Invoke-Command -ComputerName SRV1,SRV2 -FilePath C:\Scripts\myscript.ps1
When Invoke-Command runs, it will copy the PowerShell script to the remote computer and execute it just as if the administrator was on the remote computer’s local console.
4. Verify the Result
When the script finishes running, an admin can then verify the result. Again, using Invoke-Command, connect to the remote computer. This time, however, use the Scriptblock parameter instead of the file path parameter.
For example, if script.ps1 was supposed to create a file, an admin could verify this file was created using the following command:
Invoke-Command -ComputerName SRV1,SRV2 -Scriptblock { Test-Path -Path 'C:\file.txt' }