Creating and Managing Google Compute Engine Instances

This page shows how to create and manage Google Compute Engine from within Google Cloud Tools for PowerShell. Read the Cloud Tools for PowerShell cmdlet reference to learn more about Compute Engine cmdlets.

Creating an instance configuration

Before creating an instance, you must first create an instance configuration. At minimum this requires a name, a machine type, and a boot disk image or preexisting boot disk:

$disk = Get-GceImage "windows-cloud" -Family "windows-2012-r2"
$config = New-GceInstanceConfig "my-vm-1" `
    -MachineType "n1-standard-4" `
    -DiskImage $disk

See the Cloud Tools for PowerShell cmdlet reference for the other configuration options offered by the New-GceInstanceConfig cmdlet.

Creating an instance

Use the Add-GceInstance cmdlet to create a new machine instance. You can specify a project and zone, but if omitted, the parameter values default to whatever is the active Cloud SDK configuration:

$config | Add-GceInstance -Project $project -Zone us-central1-b

Managing an instance

Use the Get-GceInstance cmdlet to retrieve a project’s virtual machine instances. Since the instance name may not be unique across projects or zones, you can specify a Project or Zone parameter to narrow the search. By default the cmdlet uses whatever values are in the active Cloud SDK configuration:

$instance = Get-GceInstance "my-vm-1"

You can start, stop, or restart an instance using various cmdlets. You can refer to an instance by using the name or the strongly-typed object returned from the Get-GceInstance cmdlet:

Stop-GceInstance $instance
Start-GceInstance $instance
Restart-GceInstance "my-vm-1"

You can set instance tags, disks, access configs, and other metadata after creating your instance with the Set-GceInstance cmdlet:

Set-GceInstance $instance -AddMetadata @{"newKey" = "newValue"}
Set-GceInstance $instance -RemoveMetadata "newKey"
Set-GceInstance $instance -RemoveTag "beta" -AddTag "alpha"

Finally, when you are finished with an instance, you can remove it from Compute Engine by using the Remove-GceInstance cmdlet:

# Remove all instances with the "to-be-removed" tag.
Get-GceInstance -Project $project |
    Where { $_.Tags.Items -contains "to-be-removed" } |
    Remove-GceInstance -WhatIf

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Cloud Tools for PowerShell