This script provides a fast, project-wide inventory of your Google Compute Engine virtual machines. It resolves the project from the GCP_PROJECT environment variable, falling back to whatever is set in your active gcloud configuration, and refuses to run if neither is available. It confirms the gcloud CLI is installed, then calls compute instances list. Because the command searches all zones by default, you see every VM regardless of where it lives. The real power is in the --format flag, which builds a custom table. We use the basename() transform to strip the long resource URLs from the zone and machine type fields, leaving short readable values. We also reach into the first network interface to surface both the internal IP and, where present, the external NAT IP, relabelling the columns for clarity. Results are sorted by name for predictable scanning. This read-only command is ideal as the first step in audits, capacity reviews, or cleanup planning.
List GCP compute instances
Show every Compute Engine VM in a project with zone, machine type, status, and IPs.
13 views
Share
Script
Download .shbash
#!/usr/bin/env bash
# List all Compute Engine instances across every zone in a GCP project.
set -euo pipefail
PROJECT="${GCP_PROJECT:-$(gcloud config get-value project 2>/dev/null)}"
command -v gcloud >/dev/null 2>&1 || { echo "gcloud CLI not found"; exit 1; }
[ -n "${PROJECT}" ] || { echo "No GCP project set"; exit 1; }
echo "Compute Engine instances in project: ${PROJECT}"
# --format with a table makes the output clean and column-aligned.
# Listing without --zones searches all zones automatically.
gcloud compute instances list \
--project="${PROJECT}" \
--format="table(
name,
zone.basename(),
machineType.basename(),
status,
networkInterfaces[0].networkIP:label=INTERNAL_IP,
networkInterfaces[0].accessConfigs[0].natIP:label=EXTERNAL_IP
)" \
--sort-by=nameHow to run
Review the script first, then download or copy it and run it in your environment.
You might also like
bashlinuxAdvanced
Clean up old AMIs and snapshots
Deregister your old AMIs past a retention window and delete their backing EBS snapshots.
pythoncross-platformIntermediate
Bulk Download Images from a CSV of URLs
Download many images concurrently from a CSV list of URLs, with safe file naming and per-request timeouts.
bashlinuxIntermediate
Backup All PostgreSQL Databases with pg_dumpall
Dumps the entire PostgreSQL cluster, including roles, to one compressed file.