Se connecter à des VM Windows à l'aide de PowerShell
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Windows
Ce document explique comment se connecter à une instance de machine virtuelle (VM) Windows à l'aide de PowerShell.
Avant de commencer
Si ce n'est pas déjà fait, configurez l'authentification.
L'authentification valide votre identité pour accéder aux services et aux API Google Cloud . Pour exécuter du code ou des exemples depuis un environnement de développement local, vous pouvez vous authentifier auprès de Compute Engine en sélectionnant l'une des options suivantes :
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and
APIs, you don't need to set up authentication.
gcloud
Installez la Google Cloud CLI.
Une fois que la Google Cloud CLI est installée, initialisez-la en exécutant la commande suivante :
Si vous disposez d'un poste de travail Windows avec PowerShell, vous pouvez vous connecter à vos instances Windows Server via une session PowerShell distante.
Sur votre poste de travail local, ouvrez le terminal PowerShell.
Facultatif : Vous pouvez initialiser une variable qui conservera vos identifiants et vous évitera de les saisir chaque fois que vous vous connectez à l'instance.
Si vous ignorez cette étape, vous serez invité à saisir votre nom d'utilisateur et votre mot de passe ultérieurement.
Remplacez IP_ADDRESS par l'adresse IP externe, le nom DNS ou le nom de l'ordinateur Windows pour l'instance à laquelle vous souhaitez vous connecter.
Une fois la connexion établie, l'invite de commande change pour inclure l'adresse IP de l'instance Windows distante. Vous pouvez maintenant vous servir du terminal pour exécuter des commandes PowerShell sur l'instance Windows Server à distance.
Appeler des commandes sur votre VM Windows Server à distance
Au lieu de la commande Enter-PSSession, vous pouvez exécuter Invoke-Command avec l'option -ScriptBlock pour exécuter des commandes PowerShell sur l'instance à distance sans établir de session interactive.
IP_ADDRESS : adresse IP, nom du DNS ou nom de l'ordinateur Windows pour l'instance à laquelle vous souhaitez vous connecter.
SCRIPT : une ou plusieurs commandes à exécuter sur l'instance distante. Par exemple, spécifiez Get-EventLog -log "Windows PowerShell" pour obtenir la liste des événements du journal.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eThis document guides you through connecting to a Windows virtual machine (VM) instance using PowerShell, enabling remote management and command execution.\u003c/p\u003e\n"],["\u003cp\u003eBefore connecting, ensure you have set up authentication via the Google Cloud CLI or are using the Google Cloud console, and have created or reset your Windows password on the remote instance.\u003c/p\u003e\n"],["\u003cp\u003eConnecting to a Windows Server instance requires opening port 5986 in your Google Cloud VPC network's firewall rules, in order to accept remote PowerShell connections.\u003c/p\u003e\n"],["\u003cp\u003eYou can establish either an interactive PowerShell session using \u003ccode\u003eEnter-PSSession\u003c/code\u003e or remotely invoke commands on the VM without an interactive session by using the \u003ccode\u003eInvoke-Command\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eWhen a user connects to a VM, they can utilize all IAM permissions granted to the service account attached to that specific VM.\u003c/p\u003e\n"]]],[],null,["# Connect to Windows VMs using PowerShell\n\nWindows\n\n*** ** * ** ***\n\nThis document describes how to connect to a Windows virtual machine (VM)\ninstance by using PowerShell.\n| **Note:** When a user connects to a VM, that user can use all of the IAM permissions granted to the service account attached to the VM.\n\nBefore you begin\n----------------\n\n- If you haven't already, set up [authentication](/compute/docs/authentication). Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:\n\n Select the tab for how you plan to use the samples on this page: \n\n ### Console\n\n\n When you use the Google Cloud console to access Google Cloud services and\n APIs, you don't need to set up authentication.\n\n ### gcloud\n\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\nConnect using PowerShell\n------------------------\n\nIf you have a Windows workstation with PowerShell, you can connect to your\nWindows Server instances through a remote PowerShell session.\n\n1. If you have not created a username and password on the remote Windows\n instance yet,\n [create or reset your Windows password](/compute/docs/instances/windows/creating-passwords-for-windows-instances).\n\n2. [Add a firewall rule](/vpc/docs/using-firewalls) that opens port `5986` on\n the [Google Cloud VPC network](/vpc/docs) where your\n Windows Server instance is located.\n\n3. On your local workstation, open the PowerShell terminal.\n\n4. Optional: You can initialize a variable to hold your user credentials\n so you do not need to enter them each time you connect to the instance.\n If you skip this step, you receive a prompt for your username and password\n later.\n\n ```\n $credentials = Get-Credential\n ```\n5. Choose whether you want to\n [establish an interactive Powershell session](#interactive), or\n [invoke commands on your Windows Server VM remotely](#invoke-remotely).\n\n### Establish an interactive PowerShell session\n\nTo establish a PowerShell session, run the following command: \n\n```\nEnter-PSSession -ComputerName IP_ADDRESS -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credentials\n```\n\nReplace \u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e with the external IP address,\nDNS name, or Windows computer name for the instance to which you want to\nconnect.\n| **Note:** You can find the external IP address of your Windows instance either in the Google Cloud console on the [**VM Instances**](https://console.cloud.google.com/compute/instances) page, or in the Google Cloud CLI by running `gcloud compute instances list`.\n\nAfter you connect, the command prompt changes to include the IP address of the\nremote Windows instance. You can now use the terminal to run PowerShell\ncommands on the remote Windows Server instance.\n\n### Invoke commands on your Windows Server VM remotely\n\nAs an alternative to the `Enter-PSSession` command, you can run\n[`Invoke-Command`](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/invoke-command)\nwith the `-ScriptBlock` flag to execute PowerShell commands on the remote\ninstance without establishing an interactive session. \n\n```\nInvoke-Command -ComputerName IP_ADDRESS -ScriptBlock { SCRIPT } -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credentials\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e: the IP address, DNS name, or Windows computer name for the instance to which you want to connect.\n- \u003cvar translate=\"no\"\u003eSCRIPT\u003c/var\u003e: one or more commands to run on the remote instance. For example, specify `Get-EventLog -log \"Windows PowerShell\"` to get a list of log events.\n\nWhat's next\n-----------\n\n- Learn how to [manage access to instances](/compute/docs/instances/managing-instance-access) for multiple users across a project or organization.\n- [Securely connect to VM instances](/solutions/connecting-securely).\n- [Transfer files to your Linux instances](/compute/docs/instances/transfer-files)."]]