The ultimate guide to setting up the Azure CLI on Windows

Brian Benz
Azure Developers
Published in
4 min readMar 17, 2017

--

The Azure Command line interface (CLI) is a great way to leverage the power of Azure from the command line, on Mac, Linux and Windows. But to realize even more potential it’s best to run the CLI in an environment that accesses Linux command line tools like grep, awk and sed. On windows, that means setting up the CLI to work with Bash on Windows. Below is a handy guide to making that happen, with a few tips and tricks to get you started quickly.

You can install the CLI on whatever platform you use, including the Windows command line. However, when you try to execute a CLI command that uses Linux tools, you’ll likely get an error message, like this:

az vm list — query “[].{name:name,os:storageProfile.osDisk.osType}” — out tsv | grep Linux‘grep’ is not recognized as an internal or external command, operable program or batch file.

I say you’ll likely get this message, because if you have git BASH or Cygwin installed, the operation may work. But the only supported way to integrate Linux command line tools and the Azure CLI on Windows is by installing Bash on Windows, then installing the CLI in that bash environment.

The first step is to install bash on windows via these instructions. Once you have that set up, you can open Bash on Windows from a new Ubuntu icon added during the setup. You can also type bash from the regular command line (and exit to get back):

Note that I’m talking about the regular Windows command line and Bash as two different things — this is important because they are two different things. The official name is Bash on Ubuntu on Windows, and it’s actually Ubuntu Userland running inside Windows. Here’s a good overview of the details from Linux.com and Ubuntu Insights.

Now that bash is running on Windows, and you have the CLI installed on Windows, you can use the CLI, right? Not yet.

Here’s what you get when you try to type az, the base command for the CLI:

az: command not found

Remember in the last paragraph when I said that the regular command line and bash are two different things? This distinction is important. Remember that bash on Windows is running on top of windows, not as part of Windows. You need to install packages and dependencies like Python, even if you have Python installed in Windows already, before you install the the CLI.

For installation, you use the Ubuntu Linux commands, not the Windows commands. The easiest way to do this for the CLI is to follow the apt-get steps in the install guide, noting the latest prerequisites and dependencies before starting the CLI installation.

One important tip — if any of the installation commands fail with an unable to resolve host (none) error message, you need to update your hosts file with your machine name. If you’re an experienced geek, you know the Windows hosts file is at C:\Windows\System32\drivers\etc. But that’s not the hosts file you are looking for. You need to edit the hosts file that is used by the bash environment. Here are the steps:

In the bash environment, type sudo vi /etc/hosts

add 127.0.0.1 <WindowsmachineName> to the hosts file, then type

# <escape key> :wq! to exit vi.

At this point remind yourself of the old joke:
Why are people still using vi?
Because they can’t remember how to get out of it.

OK, now back to work — To verify the Windows computer name was added, type:

cat /etc/hosts

Your hosts file should now contain these two entries:

127.0.0.1 localhost
127.0.0.1 <WindowsMachineName>

After completing the rest of the apt-get steps in the install guide, you should be able to type az and see this screen:

Azure CLI

Now you’re running the CLI on Bash, and have access to all the power of Azure from the CLI, and Linux command line tools from bash on Windows!

One last tip — you can access files on your local machine from Bash for Windows by using the /mnt directory. For example, if your documents directory is in the usual location in Windows (usually C:\Users\<username>\Documents ), you can access it here:

/mnt/c/Users/<username>/Documents

Now you’re ready to get started with the next recommended step — working with our documentation and samples. Also, if you’re new to bash, search “bash cheat sheet”, pick one that is right for you, and keep it handy. The StackOverflow azure-cli and bash tags could also be your new best friends. For direct feedback, you can also email the CLI team with CLI questions at azfeedback@microsoft.com.

Enjoy!

--

--