Menghubungkan ke VM Windows menggunakan PowerShell
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Windows
Dokumen ini menjelaskan cara terhubung ke instance virtual machine (VM)
Windows menggunakan PowerShell.
Sebelum memulai
Siapkan autentikasi, jika Anda belum melakukannya.
Autentikasi memverifikasi identitas Anda untuk mengakses Google Cloud layanan dan API. Untuk menjalankan
kode atau sampel dari lingkungan pengembangan lokal, Anda dapat melakukan autentikasi ke
Compute Engine dengan memilih salah satu opsi berikut:
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
Menginstal Google Cloud CLI.
Setelah penginstalan,
lakukan inisialisasi Google Cloud CLI dengan menjalankan perintah berikut:
Opsional: Anda dapat menginisialisasi variabel untuk menyimpan kredensial pengguna
sehingga Anda tidak perlu memasukkannya setiap kali menghubungkan ke instance.
Jika melewati langkah ini, Anda akan menerima permintaan untuk memasukkan nama pengguna dan
sandi.
Ganti IP_ADDRESS dengan alamat IP eksternal,
nama DNS, atau nama komputer Windows untuk instance yang ingin
Anda hubungkan.
Setelah Anda terhubung, command prompt akan berubah untuk menyertakan alamat IP
instance Windows jarak jauh. Anda sekarang dapat menggunakan terminal untuk menjalankan
perintah PowerShell pada instance Windows Server jarak jauh.
Memanggil perintah di VM Windows Server dari jarak jauh
Sebagai alternatif perintah Enter-PSSession, Anda dapat menjalankan
Invoke-Command
dengan flag -ScriptBlock untuk mengeksekusi perintah PowerShell pada instance
jarak jauh tanpa membuat sesi interaktif.
IP_ADDRESS: alamat IP, nama DNS, atau nama komputer
Windows untuk instance yang ingin Anda hubungkan.
SCRIPT: satu atau beberapa perintah untuk dijalankan pada instance
jarak jauh. Misalnya, tentukan Get-EventLog -log "Windows PowerShell" untuk
mendapatkan daftar peristiwa log.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]