9

I am asking this question as Windows user but I request you not to limit the answer for windows only.

I tried executing Azure CLI commands in Power-Shell and they are executing successfully but not the other way around. In that case why do we have 2 separate command sets? Why not just work on Power-Shell? I have only tried some basic commands and they are all working except filter commands | find only works on CLI and | Select only works in Power-Shell.

I know that Azure CLI is for cross platform support. But is there any difference for Windows user? Are there any consequences to run CLI command on Power-Shell?

Thanks in advance.

Devang
  • 299
  • 2
  • 4
  • 14

5 Answers5

8

Here are my opinions from using both. In no way am I saying one is better than the other. They both have their pros and cons.


Azure CLI is cross-platform command-line tool for managing Azure resources, and it can run in Windows, Mac and Linux. This also means it can run on Windows PowerShell. Its more flexible than Azure PowerShell since its a binary and can run inside any OS default shell.

Are there any consequences to run CLI command on Power-Shell?

Updating can be a bit of a pain. If you want to update it on Windows you have to re-install the MSI with the instructions from Install Azure CLI on Windows. Updating is easier on other platforms, and makes me only want to only use Azure PowerShell on Windows PowerShell. A work around is to use WSL on Windows, then you can run Azure CLI on Linux inside a Windows machine. You can install WSL at Windows Subsystem for Linux Installation Guide for Windows 10. I find updating the azure-cli package much easier on Linux using apt-get than the Windows equivalent. You can have a look at Install Azure CLI with apt on how to install the Azure CLI package on Linux.

Another difference is that you have to chain multiple commands with Azure CLI, such as az group list vs Get-AzResourceGroup from Azure PowerShell. You also can't run Get-Help with Azure CLI commands like you do with Azure PowerShell, which is a huge game changer for me, since I find the PowerShell help system to be very helpful displaying in-depth information about PowerShell Cmdlets. The Azure CLI help info is found with az --help, but is not as comprehensive as Get-Help.

Tab completion also doesn't work in Azure CLI when using Windows PowerShell. This makes typing a bit of a pain as well, another reason I use WSL inside of Windows. You can have a look at the other alternatives at this Autocompletion support in Windows command line GitHub issue.


Azure PowerShell on the other hand is a set of PowerShell Cmdlets for managing Azure resources from the command-line, and only works in Windows PowerShell and PowerShell Core. This also means that if another OS such as Mac or Linux is running PowerShell Core, then it can run Azure PowerShell as well.

I have only tried some basic commands and they are all working except filter commands | find only works on CLI and | Select only works in Power-Shell.

Select-Object or the shorthand Select is a PowerShell Cmdlet, so it only works on PowerShell objects. find can search a string or text file. Furthermore, if you are searching for data from Azure CLI, you should use the --query parameter instead of find, since find is limited to searching strings inside text. Azure CLI uses the JMESPath query language to search for data inside the JSON output you receive. If your comfortable with this query language then searching for data using Azure CLI shouldn't be too much of an issue. Additionally, you can also use Azure CLI commands inside PowerShell scripts, but not vice-versa.


Summary

If you deal with multiple platforms or want to write scripts with others that use different platforms, using Azure CLI is a good choice. However, if you mainly deal with Windows systems and work with others who do so as well, then using Azure PowerShell is a good idea. If your like me and have to use different platforms, then installing both is a good idea. If you still just want to use Azure PowerShell on different OS platforms, then you need PowerShell Core.

For simple tasks, like quickly looking up resources in cloud shell or writing quick scripts, Azure CLI is good to use and less verbose than Azure PowerShell. If you already use bash a lot, this will feel more natural, and adding Azure CLI commands to existing scripts will be a simple task. As others have also said, there is nothing stopping you from adding Azure CLI commands to powershell scripts, which allows you to deserialize the JSON output using ConvertFrom-Json into a PSCustomObject.

For more complex tasks, Azure PowerShell is preferable, since working with .NET objects/OOP principals is much easier than parsing the JSON text given from Azure CLI. This is one reason I try to use Azure PowerShell when I can.

Azure CLI does benefit from being idempotent, so running the same command against the resources won't require any null checking like in Azure PowerShell. If this becomes an issue, then you can run ARM templates in Azure PowerShell, which are idempotent.


Update

As @AimusSage helpfully pointed out in the comments, PowerShell 7.0 has recently been released, replacing the name PowerShell Core or PowerShell 6.x. You can read more at Announcing PowerShell 7.0.

Another Idea

If you want to maintain OOP principals from Azure PowerShell, but use something that is easier for Linux Sys Admins to use, then you can consider using the Azure SDK for Python. I have used this in the past when I wanted to run scripts in a Linux host, but didn't want to use Azure CLI or install PowerShell.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 1
    Just to add on this, they recently released powershell 7, ditching the name Core. See also [this blogpost] (https://devblogs.microsoft.com/powershell/announcing-powershell-7-0/) – AimusSage Mar 15 '20 at 06:56
2

I like the previous answers, I just want to add a different point of view for people in the enterprise world that is forced to pick one:

In that case why do we have 2 separate command sets? Why not just work on Power-Shell?

Rephrasing: Both Az CLI and Az PowerShell just call the same set of APIs, the Azure APIs. This is important because theoretically and eventually you will be able to do everything in both ways

So why did Microsoft creates and maintains two ways of doing the same thing?

Martin Fowler ones said: but remember, the skill of the team will outweigh any monolith/microservice choice

If you change the monolith/microservice by PowerShell/Bash then this answers the question.

I believe that there are people with decades of development of systems using Bash, and there are other teams that are heavy users of PowerShell. Microsoft does not want them to have to learn a whole new programming language to be able to use Azure.

Summary:

If your team is familiar with PowerShell, go with PowerShell and do as much native PowerShell stuff as possible. This way you benefit from things like error handling, OOP concepts,environment settings, parallelization, etc

If yout team are linux admins, heavy users of jenkins, with million lines of Bash to automate other things already there, and working with Bash for their entire life, go with CLI and keep consistency across all the already built tools

Carlos Garcia
  • 2,771
  • 1
  • 17
  • 32
  • 1
    that is true, that's why I never pointed out which one is better (because none is really), they are different – 4c74356b41 Mar 15 '20 at 18:17
  • The more I use Azure Devops, the more insistencies I find between the 2 platforms. The PWSH modules and AZ CLI may ( ultimately ) call the same Azure APIs, but they do different things on the way there. Not all parameters are handled the same way it seems, particularly with WebApps and ACR containers. Most of our PWSH scripts for WebApp stuff uses the az cmds, not the Az PWSH module "equivalent" – Robert Slaney Mar 29 '21 at 05:38
  • @RobertSlaney you are correct, and this depends on the service. That is the reason why I sayd "eventually". but, for example, AKS is (and will be) very much CLI oriented, and if you want to operate with PS, you will find a lot of cli cmds as well. This is also because native Kubernetes uses bash (kubectl) - other Azure Services are much better from PS. At least running Bash from PS is much better experience than the other way around – Carlos Garcia Apr 30 '21 at 23:26
0

I've noticed when doing the MS Azure training (AZ-900 fundamentals and AZ-303 Azure Architect) is that the exercises are done in Azure CLI.

That's not to say it's better, but if you're wanting to do the exams it might be worth being familiar with it. For the record, I'm a PowerShell guy.

KERR
  • 1,312
  • 18
  • 13
-1

the existing answer is silly way of looking at this question. and misleading.

The biggest difference is that azure cli is a binary (that can run on different platforms) and Powershell is a shell that works across platforms. Azure Powershell is a bunch of Powershell modules, everything else derives from that.

find cannot work in the cli, because there is no in the cli, because its not a shell. find works perfectly fine in Powershell on Windows, because its a binary in the Windows OS, whereas select is a cmdlet in Powershell and hence it will not work in command line on Windows (or bash on Linux).

Furthermore, if you are searching for data from Azure CLI, you should use the --query parameter instead of find

this is also debatable, JMESPath query language is overcomplicated for no particular reason and I dont know how Azure Cli is actually compatible with the official JMESPath documentation. I prefer to use Powershell to run Azure Cli commands and just parse output JSON with Powershell. Obviously, you might not be as comfortable with Powershell as I am and you might not find this convenient.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • for the reasons specified in the answer. you are not actually specifying the main difference between those explicitly and that is a big deal. as for the parsing of the output - there are many reasons to do that, there are things you can do with Azure Cli only (and vice versa) and really doing how hard is it to add `| ConvertFrom-Json`? Besides, why learn another tool when you can use existing one (which is conveniently more flexible than JMESPath queries). Example: find all vms with a specific tag and all with a specific sku and tag and save those into 2 different variables in 1 cli query – 4c74356b41 Mar 15 '20 at 08:12
  • I like both answers. From a technical point of view @4c74356b41 mentions the root caus: one is a binary the otherone is not - that brings bunch of consequences that both of you describe in different ways. There is part of it that generates this discussion - which on is better for an specific task? or even worse: which one is better for me? ... all answers are correct for some scenario for some person (that is why i upvoted both) – Carlos Garcia Mar 15 '20 at 08:36
  • I dont mention which one is better, because theres no answer to this, but what is important is the binary\shell distinction. also, some operations are only possible in one and not the other, which is also a valid reason to be **forced** to use Powershell or cli – 4c74356b41 Mar 15 '20 at 08:41
  • 1
    Sure, if you have such a niche case then yes. If it is Azure stuff you can always just call the plain API, ts is not better than installing PS/Cli, but sometimes you are also forced because your enterprise is blocking you access do do so. My point is that if you want to find a corner case where technically you are force to use one or the other, you will find it. There is another aspect wich it is: "which knowledge your team already have? does it make sense to learn this big new thing? or i better invest somewhere else?" – Carlos Garcia Mar 15 '20 at 09:22
  • well, the existing answer doesnt mention that either. but the person claims there is no valid reason to use powershell along with azure cli which is obviously nonsense (he deleted the comment already). – 4c74356b41 Mar 15 '20 at 10:04
  • when you need functionality only available in azure powershell for one thing and one available in cli for another – 4c74356b41 Mar 15 '20 at 10:30
  • why would i bother to search for things not available in the one and available for the other? I know for sure powershell is missings most of the `az aks` commands, but I'm mostly using powreshell, not cli so have no idea what is missing from that, but obviously something is missing. all azure tools are missings some of the api calls. probably easier to look at the python sdk to figure out what its missing compared to the powershell, since cli would be missing that for sure – 4c74356b41 Mar 15 '20 at 10:56
-1

Another issue with Azure Powershell that does not seem to be the case with azure cli is the different versions. I have spent days figuring out which version of which command for which script in my pipeline needs to be what. Seriously the most ridiculous assinined

  • This does not provide an answer to the question. You can [search for similar questions](https://stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](https://stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](https://stackoverflow.com/tour) – phoenixstudio Jan 26 '21 at 19:39