PowerShell Invoke-Command

The Invoke-Command cmdlet in PowerShell executes commands on local and remote computers and returns the output with the errors from the command.

We can also use this cmdlet on the local computers to the script blocks as a command.

Syntax

Parameters

-ScriptBlock

This parameter is required, and it specifies the command to execute. When we create a script block, we need to enclose the commands in curly braces {}.

-NoNewScope

It was introduced in PowerShell 3.0 and identifies that this cmdlet executes the given command in the current scope. By default, invoke-command executes the commands in their scope.

-InputObject

This parameter specifies the input to the command. When we use this parameter in the command, we need to use the $Input automatic variable in the value of the -ScriptBlock parameter to denote the input objects.

-ArgumentList

This parameter supplies the local variable values in the command in the same order as they are listed. You must use the following format to use the local variable in the command:

-Session

This parameter specifies the array of sessions in which the invoke-command executes the command.

-FilePath

This parameter specifies the path or the file name of the local script that the invoke-command executes on one or more remote computers. If you use this parameter, PowerShell converts the content of the given script file to a script block and then transmits it to the remote computer for execution.

-ThrottleLimit

This parameter shows you a maximum number of concurrent connections which are established to execute the invoke-command. It applies only to the current command, not to the computer or to the session.

-AsJob

If you use this parameter, the invoke-command executes the commands as a background job on a remote computer.

-HideComputerName

This parameter omits the computer name of each object from the output display. It does not change the object but affects only the output display.

-JobName

This parameter specifies the friendly name for the background job. When this parameter is used in a command, the command is executed as a job, and Invoke-command returns an object of job.

-ComputerName

This parameter specifies the computers on which the command executes. The local computer is the default. When we use this parameter, PowerShell creates a temporary connection which is used only to execute the commands. If you need a permanent connection, use the -Session parameter.

-Credential

This parameter permits the user account to perform this action. The default is the current user.

-Port

The Invoke-Command uses -Port parameter to define the network ports on a remote computer, which is used for the Invoke-Command.

-UseSSL

The Invoke-Command uses -UseSSL parameter to establish a connection to the remote computer using the SSL protocol. It is an additional protection which sends the data across an HTTPS instead of HTTP.

-ConfigurationName

The Invoke-Command uses -ConfigurationName parameter to specify a session configuration which is used for the new PSSession.

-ApplicationName

The Invoke-command uses this parameter to identify the name segment of the application of the connection URI. If you are not using the -ConnectionURI parameter in the command, then use it.

-InDisconnectedSession

The Invoke-command uses this parameter to execute the command or a script in a disconnected session. When you use this parameter, the invoke-command creates the permanent session on each remote computer and starts the command specified by the -FilePath parameter, and after then disconnects from the session.

-SessionName

This parameter is used to specify a friendly name for a disconnected session. It is only valid with the -InDisconnectedSession parameter.

-SessionOption

The -SessionOption parameter is used in this command to specify the advance option for the session.

-Authentication

This parameter is used to specify the mechanism which is used to authenticate the credentials of a user. Following are the acceptable values for this parameter:

  • Default (It is a default value)
  • Kerberos
  • Negotiate
  • Basic
  • Digest
  • NegotiateWithImplicitCredential
  • Credssp

-EnableNetworkAccess

The use of this parameter in this cmdlet to include the tokens of interactive security to the loopback sessions.

-CertificateThumbprint

This parameter is used to specify the public key certificate of a user account which has permission to connect to the disconnected session.

-ConnectionUri

This parameter is used to specify the Uniform Resource Identifier (URI), which defines the connection endpoints of the session. If the ConnectionUri is not specified in the command, use the -Port and UseSSL parameters to specify the values of connection URI.

-AllowRedirection

This parameter allows redirection of this connection to an alternate URI. When we use the -ConnectionURI parameter in the command, then the remote destination can return the instructions to redirect to different URI. But we use this parameter to allow it to redirect the connection.

-VMId

This parameter is used in the Invoke-Command to specify an array of IDs of the virtual machines.

-VMName

This parameter is used in theInvoke-Command to specify an array of names of the virtual machines.

-ContainerId

This parameter is used to specify the array of container IDs.

-RunAsAdministrator

This parameter is used in the Invoke-Command to invoke the command as an administrator.