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 [VM_NAME] `
    -MachineType [MACHINE_TYPE] `
    -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 [ZONE]

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 [VM_NAME]

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 [VM_NAME]

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 [METADATA_TAG]
Set-GceInstance $instance -RemoveTag [TAG] -AddTag [TAG]

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" } |

Send feedback about...

Cloud Tools for PowerShell