Get Azure Subscriptions through PowerShell alias

Humans don’t excel at performing repetitive manual work. 

Bernard Golden

Alias in PowerShell is a fantastic way to define your own name for a cmdlet or function or script of exec file. To have it available each time you run PowerShell, you can easily add it to your profile file and it will run each time you open you u PowerShell command line.

With this technique you can then easily create aliases which solve tedious tasks, like getting to specific folder or listing out all Azure subscriptions without writing whole scripts. Just your own nickname for example azacc and voila, you get back all subscriptions for Azure environments.

Quite neat, right?

Command to run

PowerShell contains multiple ways of creating (maintaining) aliases. The most common is New-Alias, but more efficient is Set-Alias (creates or changes an alias).

To get all the subscriptions in our Azure environments, we can use az cli commands:

az account list --output table

The result should be something like this:

Azure Subscriptions overview through az cli

Exactly what we need. We can then easily use search to get needed details like which subscriptions are active and get the subscription id.

For example:

az cli with sls

Writing az account list frequently can be time consuming and not necessary. We could use something like text expander or AutoHotKey, but the idea is to have it available as alias as we can then easily copy to other environments / VMs, like dev / test / production, cloud shell, etc.

Alias it is 🙂

Let us create some aliases

We have the command we would like to execute, and we want to use aliases. To have it permanent, we would like to put it to our $PROFILE script.

$PROFILE script on the Windows operating system

Let’s use Set-Alias for the script:

Set-Alias -Name azlist -Value "az account list --output table"

And then run it to test it out:

Set-Alias throws an error

Weird, right? Reason behind: Alias doesn’t accept parameters. In short – you can alias az, but not what goes behind it. So, how to alias command, which has parameters (for example cd c:\somefolder)?

Solution with PowerShell function

If you read the error, it says cmdlet, function, script file etc. We can of course always create script files, but that means we need to then copy the files to another machine (which means additional overhead) just for that one liner. It is of course a solution, but there is a more optimized way to solve the challenge.

PowerShell functions comes to the rescue.

Function ShowAzListAccounts(){ az account list --output table}

and then calling the function results in what we need:

Az list with PowerShell function

Now we can easily define alias:

Set-Alias -Name azlist -Value ShowAzListAccounts

And then called:

AzList as Powershell function

To make it permanent, we can then easily add it to $PROFILE script and have it available each time PowerShell runs (example below):

adding it to profile page

The same technique can also be used for navigating to specific folder:

Function GoToWork(){ cd c:\Work}
Set-Alias -Name work -Value GoToWork

Conclusion

Aliases are a fantastic way to customize the workflows and shorten the execution of common tasks. With that you can easily achieve required tasks without remembering long commands or path or tools. Just your own custom commands 🙂