Using PowerShell in Cloud Shell

Posted on

One of the less well known features of Google Cloud Shell is that it has PowerShell preinstalled. All it takes to convert your Cloud Shell session into a PowerShell session is to run pwsh:

PowerShell running in Cloud Shell

Cloud Shell is based in Linux, so what you see is the .NET Core-based version of PowerShell (née PowerShell Core) as opposed to the .NET Framework-based version that you typically use on Windows.

Some PowerShell modules still only target .NET Framework or depend on certain Windows features. These modules won’t work in Cloud Shell. This might sound like a major limitation, but in practice it typically is not: Cloud Shell is external to your VPC and therefore cannot easily connect to Active Directory or WinRM anyway – as a result, many Windows-specific modules are not useful in a Cloud Shell environment anyway.

Comparing to Azure Cloud Shell

If you compare the PowerShell environment provided by Cloud Shell with Azure Cloud Shell, you will notice very few differences:

Azure Cloud Shell

Both cloud providers use Linux for their Cloud Shell and provide the latest version of PowerShell by default. The only real difference is that Azure provides a UI cue for switching between bash and PowerShell, which is pretty nice.

Using PowerShell with gcloud

Using gcloud from within PowerShell might feel a bit awkward at first, particularly when you are used to having all functionality exposed as cmdlets. However, gcloud has extensive support for output formatting and you can use this to your advantage.

To capture the value of a particular attribute into a PowerShell variable, you can use:

--format value`(attribute`)

(notice the backticks!).

Here is an example command that captures the machine type of a VM in a variable:

PS /home/jpassing> $MachineType = (gcloud compute instances describe my-vm --format value`(machineType`))
PS /home/jpassing> $MachineType
https://www.googleapis.com/compute/v1/projects/my-project-123/zones/us-central1-a/machineTypes/f1-micro

If you want to capture more complex output, you can instruct gcloud to format its output as JSON, and then use ConvertFrom-Json to turn the JSON into a PowerShell object:

PS /home/jpassing> $Instance = (gcloud compute instances describe my-vm --format json) | ConvertFrom-Json
PS /home/jpassing> $Instance.disks
autoDelete      : True
boot            : True
deviceName      : persistent-disk-0
diskSizeGb      : 10
guestOsFeatures : {@{type=VIRTIO\_SCSI\_MULTIQUEUE}}
index           : 0
interface       : SCSI
kind            : compute#attachedDisk
licenses        : {https://www.googleapis.com/compute/v1/projects/debian-cloud/global/licenses/debian-9-stretch}
mode            : READ\_WRITE
...

Configure PowerShell as your default shell

If you are ready to ditch bash in favor of PowerShell, you can add pwsh to your ~/.profile. The next time you open Cloud Shell, it will immediately launch PowerShell.

Any opinions expressed on this blog are Johannes' own. Refer to the respective vendor’s product documentation for authoritative information.
« Back to home