Recently I helped out a friend on Twitter with an Azure PowerShell issue they were having logging in to their subscriptions with the ‘Az’ PowerShell module.
It should also be noted that you can easily use other tools like Windows Terminal to access CloudShell or access it directly from https://shell.azure.com
However this scenario is for where PowerShell is required locally. And more importantly you need to avoid SSO (Single Sign-On) on devices from just bypassing the Azure AD login pages as you need to login as a different user or to a different tenant etc…
Once they had confirmed my proposed solution/idea fixed the issue with not being able to login to Azure via PowerShell and being able to access what they wanted, I thought it would be rude not share my useful tip with the rest of you; so here we go.
How I Always Login To Azure PowerShell
Below I will share with you how I login to Azure via the ‘Az’ PowerShell module every time without fail to avoid access issues.
- Open an In-Private Browser Window
N.B. make sure you don’t have any other In-Private tabs logged into Azure or Office 365 open at the same time in the same browser client when doing this.
2. Browse To: https://portal.azure.com
3. Login To The Azure Account You Want To Access Via PowerShell
4. Confirm You Are Logged In To The Correct Azure Account
5. Open PowerShell
6. Run the command: Login-AzAccount -UseDeviceAuthentication
7. You Will Be Given Instructions To Follow To Complete The Login
8. In The In-Private Browser Tab You Have Open Browse To: https://microsoft.com/devicelogin OR https://aka.ms/devicelogin
N.B. It’s covered in red on my screenshot (for security reasons)
10. Confirm The Account To Login To PowerShell With
N.B. This is the account that is already signed in from earlier
11. You Should Then See A Confirmation Page Stating You Are Logged Into Azure PowerShell
12. You Can Now Run Az PowerShell Commands
Other Useful Az PowerShell Commands
- Select-AzSubscription -SubscriptionId ‘SUBSCRIPTION-ID-GUID’
N.B. Use Get-AzSubscription to get a list of all subscriptions and their ID’s in the AAD Tenant you have logged into and have access too (as shown in step 12)
About the Author:
Hi, I’m Jack Tracey, I am 26 and live in West Sussex, England, United Kingdom.
I am currently a Solutions Architect for an MSP based in the UK (headquartered in London). I am responsible for assisting customers migrate, build & expand to mainly Azure IaaS & PaaS services as well as assisting with the various networking elements required when moving to the cloud.
I am also a co-founder of the Sussex Azure User Group where we host monthly meetups to discuss all things Azure in a safe, friendly and chilled environment. Check out our website at https://sussexazure.uk or follow us on Twitter at @SussexAzure
Tracey, J. (2019). Logging In To Azure PowerShell. Available at: https://jacktracey.co.uk/how-tos/logging-in-to-azure-powershell/ [Accessed: 6th January 2020].