PowerShell is a command-line shell and associated scripting language built on the .NET Framework. It's the default task automation and configuration management tool used in the Windows world. A PowerShell cmdlet is a lightweight command invoked within PowerShell.

Cloud Tools for PowerShell is a collection of cmdlets for accessing and manipulating Google Cloud resources such as Google Compute Engine, Google Cloud Storage, Google Cloud SQL and Google Cloud DNS —with more to come!

Follow along with this lab to learn how to interact with Google Cloud resources from PowerShell.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with Google Cloud Platform?

Novice Intermediate Proficient

For this codelab, you need a Google Cloud project to interact with PowerShell. If you have an existing project, you can either use that or you can create a new project using the following steps.

Codelab-at-a-conference setup

The instructor will be sharing with you temporary accounts with existing projects that are already setup so you do not need to worry about enabling billing or any cost associated with running this codelab. Note that all these accounts will be disabled soon after the codelab is over.

Once you have received a temporary username / password to login from the instructor, log into Google Cloud Console: https://console.cloud.google.com/.

Here's what you should see once logged in :

Note the project ID you were assigned ( "codelab-test003" in the screenshot above). It will be referred to later in this codelab as PROJECT_ID.

Once authenticated, you're ready to use Google Cloud cmdlets within PowerShell. Find and open PowerShell command-line shell.

To make sure Google Cloud cmdlets are installed correctly, run Get-GceInstance cmdlet. This should list the Compute Engine instances you have in your project.

In this section, you create and manage Google Compute Engine from within Google Cloud Tools for PowerShell.

Create 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 pre-existing boot disk. Use Get-GceImage to create a disk image and New-GceInstanceConfig to create a configuration.

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

At this point, you have a configuration that you can use to create an instance.

Create an instance

Use the Add-GceInstance cmdlet to create a new Compute Engine instance. You can specify a project and zone, but if omitted, the parameter values default to those in your current active Cloud SDK configuration. If you specify project, make sure to replace PROJECT_ID with your own project id.

$config | Add-GceInstance -Project PROJECT_ID -Zone europe-west1-b

Manage an instance

You can 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 set instance tags, disks, access configs, and other metadata after creating your instance with the Set-GceInstance cmdlet. Add some metadata and then remove as follows.

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

Now, add a tag that you will use later to remove the instance.

Set-GceInstance $instance -AddTag "to-be-removed"

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. Play with some of these commands.

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

Finally, when you are finished with the instance, remove it from Compute Engine by using the Remove-GceInstance cmdlet and supplying the tag you specified earlier.

Get-GceInstance -Project $project |
    Where { $_.Tags.Items -contains "to-be-removed" } |
    Remove-GceInstance -WhatIf

In this part of the codelab, you back up data from your local machine to Google Cloud Storage using Cloud Tools for PowerShell.

Create a bucket

Before uploading files to Cloud Storage, you need to create a bucket. Use the New-GcsBucket cmdlet to create a new bucket.

$bucket = "my-gcs-bucket"
New-GcsBucket $bucket

Upload files to the bucket

You can use New-GcsObject to upload file or entire folders to the bucket.

You can upload the contents of a local file to Cloud Storage by using the -File parameter and specifying a file path. Alternatively, you can pass the object's contents as a string via the PowerShell pipeline, or you can use the -Value parameter.

Pick a local file on your machine and upload to your bucket as follows.

New-GcsObject -Bucket "my-gcs-bucket" -File "C:\path\to\some\file\hello.txt"


You can upload an entire directory from the local disk to Cloud Storage by using the -UploadFolder parameter and specifying the folder path. If you do not want the folder to be uploaded directly to the root of the Cloud Storage bucket, use -ObjectNamePrefix to specify a prefix that will be applied to every object uploaded.

Pick a local folder on your machine and upload to your bucket as follows.

New-GcsObject -Bucket "my-gcs-bucket" -Folder "C:\path\to\some\folder"

Search data

You can search data with cmdlets, or with the provider through the common file search cmdlets. Try the following command with your bucket.

Get-GcsObject $bucket | Select Name, Size | Out-GridView

You should see a grid pop-up with name and size.

Read data

You can use the Read-GcsObject cmdlet to read data. As an example, you can use the following command to read a file named hello.txt to your desktop.

Read-GcsObject $bucket "hello.txt" `
    -OutFile "$Env:UserProfile\Desktop\hello.txt"

Delete data

Last but not least, you can delete data using the Remove-GcsObject cmdlet. Use the following command to delete all contents of the bucket.

Get-GcsObject $bucket | Remove-GcsObject

In this codelab, you learned how to manage Compute Engine instances and Cloud Storage buckets from PowerShell but there's more! You can also manage Cloud SQL and Cloud CDN resources using PowerShell. Check out the list of next steps below to learn more.

What we've covered

Next Steps