🚀 Executive Summary

TL;DR: The Azure Portal’s ‘Export Text’ is a UI feature, not an Azure CLI command, leading to messy JSON output when trying to extract clean data. The solution involves using command-line tools like `jq` for quick fixes, or more robustly, Azure CLI’s built-in `–query` (JMESPath) with `-o tsv` for structured output, or full scripting for complex automation.

🎯 Key Takeaways

  • The ‘Export Text’ functionality in Azure Portal’s Cloud Shell is a user interface feature, not an inherent Azure CLI command.
  • For immediate, ad-hoc text extraction from Azure CLI JSON output, tools like `jq` and `sed` can be used as quick fixes.
  • The recommended and most robust method for structured text output from Azure CLI is using the `–query` argument (JMESPath) combined with the `-o tsv` or `-o table` output formats.
  • For scripting and automation, `-o tsv` is preferred over `-o table` because it provides predictable, headerless, tab-separated values, making parsing easier.
  • Complex scenarios requiring chained commands, advanced logic, or error handling are best addressed by writing dedicated shell scripts (e.g., Bash) that leverage Azure CLI commands.

Is there a command equivalent to the command palette's Export Text?

Struggling to get clean, text-based output from the Azure CLI without the portal’s ‘Export Text’ button? This guide walks you through the command-line equivalents, from quick pipes to robust scripting, to format your `az` command results like a pro.

From the Trenches: Where’s the ‘Export Text’ Command in Azure CLI?

It was 2 AM. A P1 incident, of course. Our main production database, prod-db-01, was refusing connections from a newly provisioned app service. The on-call SRE, a sharp but still relatively junior engineer, was frantically trying to verify the firewall rules. He was in the Azure Cloud Shell, saw the list of rules, but his terminal was wrapping the text, and copy-pasting the JSON output was a nightmare. “I just need the clean list like when I hit ‘Export Text’!” he said over the call. That’s when it hits you: that convenience is a GUI trick, not a CLI command. We’ve all been there, staring at a wall of JSON, wishing for a simple button that doesn’t exist on our local machine. That night, we wasted 15 minutes wrestling with formatting instead of fixing the problem. Let’s make sure that doesn’t happen to you.

The “Why”: A Tale of Two Layers

First, let’s get to the root of the problem. Why isn’t there an az cli --export-text command? Because the “Export Text” button you see in the Azure Portal’s Cloud Shell isn’t part of the Azure CLI at all. It’s a feature of the portal’s user interface. The portal runs your az command, gets the structured data (usually JSON), and then uses its own front-end logic to prettify it into that clean text view. The az CLI itself is just a tool that spits out data; it’s up to you to tell it how you want that data formatted. Once you understand this separation, you can stop looking for a magic command and start using the powerful tools you already have.

Solution 1: The Quick Fix (The `jq` and `sed` Hammer)

You’re in a hurry. The heat is on. You don’t have time to read the docs on JMESPath queries. You just need to extract a value from that wall of JSON, right now. This is where you grab your trusty command-line tools like jq, grep, and sed. It’s not elegant, but it’s fast and effective.

Let’s say you need to quickly list the names and power states of all VMs in the prod-rg-westus2 resource group.

az vm list --resource-group prod-rg-westus2 --show-details --output json | jq -r '.[] | "\(.name) \t \(.powerState)"'

What’s happening here?

  • az vm list ... --output json: We run the standard command but force the output to JSON.
  • | jq -r '.[] | ...': We pipe that JSON into jq. The -r flag gives us raw string output (no quotes). We iterate through each element .[] and create a string with the VM name .name, a tab character \t, and the power state .powerState.

Is it pretty? No. Is it a permanent solution you should build scripts around? Absolutely not. But did it get you the data you needed in 30 seconds during an outage? Yes. And sometimes, that’s all that matters.

Solution 2: The Permanent Fix (The Right Way with `–query` and `-o tsv`)

Okay, the fire is out. Now it’s time to learn the correct way to do this using built-in Azure CLI features. The CLI has powerful formatting capabilities using the --query argument (which uses a syntax called JMESPath) and the --output (or -o) flag.

Let’s solve the same problem as before: get the name and power state for our VMs.

az vm list --resource-group prod-rg-westus2 --show-details --query "[].{Name:name, PowerState:powerState}" -o table

This gives you a beautiful, human-readable table:

Name                PowerState
------------------  -------------------
prod-web-vm-01      VM running
prod-worker-vm-01   VM deallocated
prod-bastion-vm     VM running

The --query argument is doing the heavy lifting. We’re telling it to create a new structure for each item in the array ([]), creating a `Name` key from the original `name` property and a `PowerState` key from the original `powerState` property. Then, -o table does the rest.

Pro Tip: For scripting and automation, the `table` output is terrible because the column widths can change. Instead, use `tsv` (Tab-Separated Values). It produces clean, predictable output without headers, perfect for feeding into a `while read` loop or other scripts.

az vm list --resource-group prod-rg-westus2 --show-details --query "[].[name,powerState]" -o tsv

Solution 3: The ‘Nuclear’ Option (Just Script It)

Sometimes, a one-liner isn’t enough. You need to get data from one command and use it in another, or you need to apply more complex logic. In this case, don’t try to build a monstrous, unreadable one-liner. Just write a simple script. It’s more maintainable, easier to debug, and clearer for the next person who has to support it.

Imagine you need to get the primary public IP address for every VM in a resource group. This requires getting the VM, then its network interface, and then the IP configuration. This is a perfect use case for a small Bash script.

#!/bin/bash

# A script to list the primary Public IP for each VM in a specified resource group.

RESOURCE_GROUP="prod-rg-westus2"
echo "Fetching IPs for VMs in Resource Group: ${RESOURCE_GROUP}"
echo "------------------------------------------------------"

# Get all VM NIC IDs in the resource group using query and tsv for clean output
nic_ids=$(az vm list -g ${RESOURCE_GROUP} --query "[].networkProfile.networkInterfaces[0].id" -o tsv)

if [ -z "$nic_ids" ]; then
    echo "No VMs or NICs found in ${RESOURCE_GROUP}."
    exit 0
fi

# Loop through each NIC ID
for nic_id in $nic_ids; do
    # Get the Public IP ID associated with the NIC's first IP config
    ip_config_id=$(az network nic show --ids ${nic_id} --query "ipConfigurations[0].publicIpAddress.id" -o tsv)

    if [ -n "$ip_config_id" ]; then
        # Get the VM Name from the NIC ID for better reporting
        vm_name=$(az vm list -g ${RESOURCE_GROUP} --query "[?networkProfile.networkInterfaces[0].id=='${nic_id}'].name" -o tsv)
        
        # Get the actual IP address from the Public IP ID
        public_ip=$(az network public-ip show --ids ${ip_config_id} --query "ipAddress" -o tsv)
        printf "%-20s %s\n" "${vm_name}:" "${public_ip}"
    fi
done

echo "------------------------------------------------------"

This approach is infinitely more flexible. You can add error handling, logging, and complex logic that would be impossible to manage in a single command. It’s the ultimate “Export Text” because you define exactly what text gets exported and how it looks.

Darian Vance - Lead Cloud Architect

Darian Vance

Lead Cloud Architect & DevOps Strategist

With over 12 years in system architecture and automation, Darian specializes in simplifying complex cloud infrastructures. An advocate for open-source solutions, he founded TechResolve to provide engineers with actionable, battle-tested troubleshooting guides and robust software alternatives.


🤖 Frequently Asked Questions

âť“ Why isn’t there a direct ‘Export Text’ command in Azure CLI?

The ‘Export Text’ button in the Azure Portal’s Cloud Shell is a feature of the portal’s user interface, not part of the Azure CLI itself. The CLI outputs raw structured data (typically JSON), and the portal’s front-end logic then formats it into a clean text view.

âť“ How do `jq`/`sed` compare to `–query` and scripting for Azure CLI output?

`jq`/`sed` are quick, ad-hoc tools for immediate text extraction from JSON, suitable for urgent situations. Azure CLI’s `–query` with `-o tsv` provides a more structured and maintainable way to extract specific data for automation. Scripting offers the highest flexibility for complex logic, error handling, and chaining multiple commands.

âť“ What’s a common implementation pitfall when trying to get clean text output from Azure CLI for scripting?

A common pitfall is using `-o table` for scripting. While human-readable, its variable column widths and headers make programmatic parsing unreliable. For scripting, always prefer `-o tsv` with `–query` as it produces consistent, tab-separated values without headers, which is ideal for automated processing.

Leave a Reply

Discover more from TechResolve - SaaS Troubleshooting & Software Alternatives

Subscribe now to keep reading and get access to the full archive.

Continue reading