Invoke Command Examples

If you aren’t using Invoke-Command on your network, you should really think about upgrading your life and enabling WinRM in your environment.

Here are a few examples of Invoke-Command used in PowerShell:
Note: replace targethostname with the hostname of the computer you are targeting.

This will get the ipconfig information of the computer.


invoke-command -ComputerName targethostname {ipconfig /all}

These two commands will get you a list of printers installed and remove printers.

invoke-command -ComputerName targethostname {get-printer | select Name}
invoke-command -ComputerName targethostname {remove-printer -Name "HP 477dw"}

If a user is having problems printing, you can remotely troubleshoot the computer by:

invoke-command -ComputerName targethostname {net stop spooler}

invoke-command -ComputerName targethostname {del C:\Windows\System32\spool\PRINTERS\*}

invoke-command -ComputerName targethostname {net start spooler}

This last command will check the Event log for issues. Specifically Event ID 41: “The system has rebooted without cleanly shutting down first”

invoke-command -ComputerName targethostname {Get-WinEvent @{logname='system';ID=41} -MaxEvents 5 | Select TimeCreated}

 

As you can see, you can run any command you would locally on your machine with Invoke-Command. All you have to do is add the wrapper { } and insert your CLI command.

Pretty cool!!

Leave A Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.