EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Invoke-Command

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Invoke-Command

powershell inoke command

Introduction to PowerShell Invoke-Command

Invoke-Command is used to run any command on a local or remote computer and return its output. It allows us to write a script or a command block and invoke that script or command block. It will return the result of command execution. This command can be run on local or remote. Let us understand it by some basic example. In very simple if We have some commands {command1 command 2 commands 3} and we want to execute them and get out of their execution than we can use PowerShell Invoke-Command.

Syntax of PowerShell Invoke-Command

Execute the below command and you will get the output as all its syntax and parameters. We show all the available syntax for PowerShell Invoke-Command. Every syntax will be used for particular situations.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Get-Help Invoke-Command -full

Below is the syntax we got after running the above command.

1. With Passing Argument to a Remote Computer

Invoke-Command
[-ScriptBlock] <scriptblock>
[-NoNewScope] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

2. With Defining Job Names

Invoke-Command
[[-Session] <PSSession[]>] [-FilePath] <string>
[-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-JobName <understandable user defined names>] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

3. Running the Commands in Remote Debugging Mode

Invoke-Command
[[-Session] <PSSession[]>] [-ScriptBlock] <scriptblock>
[-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-JobName <string>] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
[[-ComputerName] <string[]>] [-FilePath] <string>
[-Credential <pscredential>] [-Port <int>] [-UseSSL] [-ConfigurationName <string>] [-ApplicationName <string>] [-ThrottleLimit <int>] [-AsJob] [-InDisconnectedSession] [-SessionName <string[]>] [-HideComputerName] [-JobName <string>] [-SessionOption <PSSessionOption>] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-EnableNetworkAccess] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Popular Course in this category
Sale
All in One Data Science Bundle (360+ Courses, 50+ projects)360+ Online Courses | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (3,220 ratings)
Course Price

View Course

Related Courses
Shell Scripting Training (4 Courses, 1 Project)Data Visualization Training (15 Courses, 5+ Projects)

Invoke-Command
[[-ComputerName] <string[]>] [-ScriptBlock] <scriptblock>
[-Credential <pscredential>] [-Port <port where remote computer is listening>] [-UseSSL] [-ConfigurationName <string>] [-ApplicationName <string>] [-ThrottleLimit <int>] [-AsJob] [-InDisconnectedSession] [-SessionName <string[]>] [-HideComputerName] [-JobName <string>] [-SessionOption <PSSessionOption>] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-EnableNetworkAccess] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [-CertificateThumbprint <string>] [<CommonParameters>]

Invoke-Command
[[-ConnectionUri] <uri[]>] [-ScriptBlock] <scriptblock>
[-Credential <pscredential>] [-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-InDisconnectedSession] [-HideComputerName] [-JobName <string>] [-AllowRedirection] [-SessionOption <PSSessionOption>] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-EnableNetworkAccess] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [-CertificateThumbprint <string>] [<CommonParameters>]

Invoke-Command
[[-ConnectionUri] <uri[]>] [-FilePath] <string>
[-Credential <pscredential>] [-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-InDisconnectedSession] [-HideComputerName] [-JobName <string>] [-AllowRedirection] [-SessionOption <PSSessionOption>] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-EnableNetworkAccess] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <Object[]>] [<CommonParameters>]

Invoke-Command
[-VMId] <guid[]>
[-ScriptBlock] <scriptblock>
-Credential <pscredential>
[-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
[-ScriptBlock] <scriptblock>
-Credential <pscredential>
-VMName <string[]>
[-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
[-VMId] <guid[]>
[-FilePath] <string>
-Credential <pscredential>
[-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
[-FilePath] <path of the file>
-Credential <pscredential>
-VMName <string[]>
[-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
-ScriptBlock <scriptblock>
-HostName <string[]>
[-Port <port number where remote computer is listening>] [-AsJob] [-HideComputerName] [-UserName <string>] [-KeyFilePath <string>] [-SSHTransport {true}] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [-Subsystem <string>] [<CommonParameters>]

Invoke-Command
[-ScriptBlock] <scriptblock>
-ContainerId <string[]>
[-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-JobName <string>] [-RunAsAdministrator] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
[-FilePath] <path of the file>
-ContainerId <string[]>
[-ConfigurationName <string>] [-ThrottleLimit <int>] [-AsJob] [-HideComputerName] [-JobName <string>] [-RunAsAdministrator] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
-ScriptBlock <scriptblock>
-SSHConnection <hashtable[]>
[-AsJob] [-HideComputerName] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
-FilePath <path of the file>
-HostName <string[]>
[-AsJob] [-HideComputerName] [-UserName <string>] [-KeyFilePath <string>] [-SSHTransport {true}] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Invoke-Command
-FilePath <path of the file>
-SSHConnection <hashtable[]>
[-AsJob] [-HideComputerName] [-RemoteDebug] [-InputObject <psobject>] [-ArgumentList <list of arguments in object form>] [<CommonParameters>]

Top 18 Parameters of PowerShell Invoke-Command

The top 18 parameters of PowerShell invoke-command explained below.

1. AllowRedirection: Sometime when you are trying to access the remote computer, it is possible that the remote computer returns another  URI for alternate redirection. Remember PowerShell will redirect automatically if we want to redirect to the remote return URL than we should use this parameter.

2. ApplicationName: Here we can define the application name of our ConnectionURI. We should use this parameter only if we are not using ConnectionURI.

3. ArgumentList: These are the variables that we can pass to remote commands, as remote commands variables will be replaced with these variables. Pass the arguments with comma-separated.

4. AsJob: Suppose you are running some commands on the remote computers and that command or job is taking too much time, in that case, AsJob command will return some object giving the details of the running job. Simple it will allow us to run some background in case if the previous job is taking extensive time to execute.

5. Authentication: This defines a mechanism for authentication for accessing remote computers. But it is only available for Windows, Vista and Windows Server 2008.

6. CertificateThumbprint: It allows us to use some digital public key certificate for establishing a connection with remote computers.

7. ComputerName: This command defined the computers where we are going to run our commands. If we do not define –ComputerNameit will run the command on the local computer.

8. ConnectionUri: Defines a Uniform Resource Identifier (URI), which contains the endpoint of the session.

Below is the format for that:

<Transport>://<ComputerName>:<Port>/<ApplicationName>

The default value will be like:

http://localhost:8081/Ranjan

In case you do not have endpoint you can use UseSSL and Port to define your connection URI values.

9. ContainerId: Defines an array of IDs.

10. Credential: Display all user accounts which have permission to do certain actions. If we do not define any then it will give the current user only.

11. EnableNetworkAccess: Suppose we want to copy some XML files from one computer (one session) and paste it to a local computer then we can use this command. It adds a security token to loopback session which gets data from another session.

12. FilePath: If we want to access a file or script on the remote computer then we can use this command. We just have to specify the path for it.

13. HostName: Specifies an array of computer names for a Secure Shell (SSH) based connection. This is similar to the ComputerName parameter except that the connection to the remote computer is made using SSH rather than Windows WinRM.

14. JobName: It allows us to write user-friendly job names. Its simple syntax is Job<name>.

15. NoNewScope: It defines that given cmdlet will run in the current scope, if we do not specify this parameter then it will run in its own scope.

16. Port: It defines the port on which a remote computer is running. A remote port is a user remote computer. So to connect any computer we need to get a remote computer port on which it is listening.

17. RemoteDebug: It allows us to put some debugger on the remote computer for commands which we are going to execute for checking or debugging.

18. UserName: Defines the username for a remote account for which we are running the commands So for example if for are running the command for remote computer XYX than there must be a user with “XYZuser”.

Examples to Implement PowerShell Invoke-Command

Following are different examples to implement PowerShell invoke-command.

Example #1

Invoke-Command -ScriptBlock {Get-Process}

Output:

PowerShell Invoke-Command 1-1

We can run the same command on remote computers like,

Invoke-Command -ComputerName “name of host or remote computer”-ScriptBlock {$p = Get-Process PowerShell}

Example #2

Below is the example for a local computer for getting a version of computers.

invoke-command -scriptblock {(get-host).version}

Output:

PowerShell Invoke-Command 1-2

For a remote computer,

$version = Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {(Get-Host).Version}

Example #3

In the below example, we are putting Get-Host inside curly braces and on executing this command we are getting output as complete host details.

Invoke-Command -ScriptBlock {Get-Host}

Output:

 putting Get-Host

Example #4

In the below example, we are putting Get-Culture inside curly braces and on executing this command we are getting the output.

Invoke-Command -ScriptBlock {Get-Culture}

Output:

 putting Get-Culture

If you want to run the same command for any remote computers you can use,

Invoke-Command -ComputerName “remote computer name” -Credential domain\username -ScriptBlock {Get-Culture}

This command is also available for Linux and macOS. So if anyone of you is using Linux or macOS you can follow Documentations for that.

Conclusion

In case of certain defined commands(block of commands) which we want to run on either on local(same computer) or on the remote computer, we can use Invoke-Command.

Recommended Articles

This is a guide to PowerShell Invoke-Command. Here we discuss the introduction and top 18 parameters of PowerShell invoke-command with its examples. You may also look at the following articles to learn more-

  1. Parameters of PowerShell Move-Item
  2. How does Echo Work in Shell Scripting?
  3. Architecture of Kubernetes
  4. Examples of Powershell Write-Host
  5. PowerShell Get-Location | Syntax | Parameters
  6. Guide to PowerShell Test-Path
  7. PowerShell New-Item | Examples
  8. Guide to PowerShell Rename-Item
  9. Examples of PowerShell Get-Content
  10. Examples of User Defined Data Types in C++

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More