Fixing the Failed To Acquire Token Error When Logging Into Azure from PowerShell

When I haven’t used Azure from PowerShell in some time, I’ll get an error “failed to acquire token” when using cmdlets such as Get-AzSubscription or Get-AzContext.

To add to the confusion, sometimes Connect-AzAccount appears to work and log me in, but I’ll still get the error.

Under the hood, the Azure cmdlets are caching some connection tokens. Fixing this issue is pretty simple, although not intuitive or easy to find the solution. All you have to do is issue the following cmdlet:

Clear-AzContext

This will remove old tokens from your system.

After issuing the clear command, just end your current PowerShell session, then login again using Connect-AzAccount and you should be good to go.