Skip to main content
Skip to main content

gcloud CLI Examples

Audience: Cloud Engineers and Developers.

Purpose: Reference commands for common GCP operations using the gcloud CLI.


Overview

The gcloud CLI is the primary command-line interface for Google Cloud Platform. This guide provides examples for common administrative tasks.


ResourceDescription
gcloud CLI OverviewOfficial documentation
gcloud ReferenceComplete command reference
Scripting GuideAutomation best practices

Project Queries

List Projects by Billing Account

Return information about all projects linked to the TAMUNI-i2-Primary-Burwood billing account:

# Billing Account ID: 0152CF-9A23B4-75E0E7

gcloud beta billing projects list \
--billing-account="0152CF-9A23B4-75E0E7" \
--format="value(projectId)" | \
while read -r line; do
gcloud projects describe $line --format=json(projectId,projectNumber,labels)
done

Query Project Tags

Return all tags associated with projects linked to the billing account:

gcloud beta billing projects list \
--billing-account="0152CF-9A23B4-75E0E7" \
--format="value(projectId)" | \
while read -r line; do
gcloud asset search-all-resources \
--scope=projects/$line \
--asset-types=cloudresourcemanager.googleapis.com/TagKey \
--format=json
done

Output Formatting

Common format options for gcloud commands:

FormatDescriptionExample
jsonFull JSON output--format=json
yamlYAML output--format=yaml
tableTabular output--format=table
valuePlain values (for scripting)--format="value(field)"
csvCSV output--format=csv

Selective Field Output

# Return only specific fields
gcloud projects describe PROJECT_ID \
--format="json(projectId,projectNumber,labels)"

# Get just the project ID
gcloud projects list --format="value(projectId)"

Tips for Scripting

Avoid Interactive Prompts

Use --quiet or -q to suppress interactive prompts in scripts:

gcloud projects delete PROJECT_ID --quiet
Output Parsing

Use jq to parse JSON output:

gcloud projects list --format=json | jq '.[].projectId'