Connect WSMan - PowerShell


Overview

Connect-WSMan establishes a Windows Remote Management (WinRM) connection to a remote computer or a target virtual machine (VM). It allows you to perform administrative and other remote operations as if you were there locally.

Syntax

Connect-WSMan [[-ComputerName] <string> | -Computer <computername | Session>]
[-Credential <PSCredential>] [-Port <int>] [-TimeoutSec <int>]
[-Authentication <string>] [-Delimiter <string>] [-Culture <string>]
[-OperationTimeoutSec <int>] [-SkipCACheck] [-SkipCNCheck] [-SkipRevocationCheck]
[-SkipNegotiate] [-Trace] [-SkipCertVerification]

Options/Flags

  • -ComputerName (Alias -Computer): Specifies the name of the remote computer or virtual machine (VM) to connect to.
  • -Credential: Provides a PSCredential object for authentication.
  • -Port: Specifies the port number to use for the connection.
  • -TimeoutSec: Sets the session idle timeout in seconds.
  • -Authentication: Sets the authentication method (Basic, Negotiate, or Kerberos).
  • -Delimiter: Sets the delimiter used in remote commands.
  • -Culture: Sets the culture to use for remote commands.
  • -OperationTimeoutSec: Sets the timeout in seconds for remote operations.
  • -SkipCACheck: Skips verification of the certificate authority (CA).
  • -SkipCNCheck: Skips verification of the common name (CN) in the certificate.
  • -SkipRevocationCheck: Skips checking the revocation status of the certificate.
  • -SkipNegotiate: Forces the use of basic authentication instead of negotiation.
  • -Trace: Enables Windows Remote Management (WinRM) tracing.
  • -SkipCertVerification: Skips certificate verification.

Examples

Example 1: Connect to a remote computer using basic authentication

Connect-WSMan -ComputerName remotecomputer -Credential Get-Credential

Example 2: Connect with an encrypted certificate

Connect-WSMan -ComputerName remotecomputer -Credential $cred -Authentication Negotiate -SkipCNCheck

Example 3: Set a custom session timeout and delimiter

Connect-WSMan -ComputerName remotecomputer -Credential $cred -TimeoutSec 300 -Delimiter ","

Common Issues

  • Ensure that the remote computer has WinRM enabled and an appropriate firewall exception is configured.
  • Check that the user account used for authentication has sufficient privileges on the remote system.
  • Review the WinRM event logs on the remote computer for troubleshooting errors.

Integration

  • Combine with Invoke-Command to execute commands remotely.
  • Use with New-PSSession to create a persistent session for automated tasks.
  • Integrate into scripts for automating remote management tasks.
  • Invoke-Command
  • New-PSSession
  • Enter-PSSession