Creating and managing Compute Engine instances

This page shows how to create and manage Compute Engine instances from within 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 a 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 parameters, such as project, zone, or region. If you omit a parameter, then the cmdlet uses the values set in your Google Cloud CLI 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 you set in the active gcloud CLI 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" } |
    Remove-GceInstance