|
| 1 | +--- |
| 2 | +title: Sign in with Azure PowerShell |
| 3 | +description: How to sign in with Azure PowerShell as a user, service principal, or with managed identities for Azure resources. |
| 4 | +ms.devlang: powershell |
| 5 | +ms.topic: conceptual |
| 6 | +ms.date: 06/18/2020 |
| 7 | +--- |
| 8 | +# Sign in with Azure PowerShell |
| 9 | + |
| 10 | +Azure PowerShell supports several authentication methods. The easiest way to get started is with |
| 11 | +[Azure Cloud Shell](/azure/cloud-shell/overview), which automatically logs you in. With a local |
| 12 | +install, you can sign in interactively through your browser. When writing scripts for automation, |
| 13 | +the recommended approach is to use a [service principal](create-azure-service-principal-azureps.md) |
| 14 | +with the necessary permissions. When you restrict sign-in permissions as much as possible for your |
| 15 | +use case, you help keep your Azure resources secure. |
| 16 | + |
| 17 | +After signing in, commands are run against your default subscription. To change your active |
| 18 | +subscription for a session, use the [Set-AzContext](/powershell/module/az.accounts/set-azcontext) |
| 19 | +cmdlet. To change the default subscription used when logging in with Azure PowerShell, use |
| 20 | +[Set-AzDefault](/powershell/module/az.accounts/set-azdefault). |
| 21 | + |
| 22 | +> [!IMPORTANT] |
| 23 | +> Your credentials are shared among multiple PowerShell sessions as long as you remain signed in. |
| 24 | +> For more information, see the article on [Persistent Credentials](context-persistence.md). |
| 25 | +
|
| 26 | +## Sign in interactively |
| 27 | + |
| 28 | +To sign in interactively, use the |
| 29 | +[Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount) cmdlet. |
| 30 | + |
| 31 | +```azurepowershell-interactive |
| 32 | +Connect-AzAccount |
| 33 | +``` |
| 34 | + |
| 35 | +When run from PowerShell version 6 and higher, this cmdlet presents a token string. To sign in, copy |
| 36 | +this string and paste it into [microsoft.com/devicelogin](https://microsoft.com/devicelogin) in a |
| 37 | +web browser. Your PowerShell session will be authenticated to connect to Azure. You can specify the |
| 38 | +`UseDeviceAuthentication` parameter to receive a token string on Windows PowerShell. |
| 39 | + |
| 40 | +> [!IMPORTANT] |
| 41 | +> Username/password credential authorization has been removed in Azure PowerShell due to changes in |
| 42 | +> Active Directory authorization implementations and security concerns. If you use credential |
| 43 | +> authorization for automation purposes, instead |
| 44 | +> [create a service principal](create-azure-service-principal-azureps.md). |
| 45 | +
|
| 46 | +Use the [Get-AzContext](/powershell/module/az.accounts/get-azcontext) cmdlet to store your tenant ID |
| 47 | +in a variable to be used in the next two sections of this article. |
| 48 | + |
| 49 | +```azurepowershell-interactive |
| 50 | +$tenantId = (Get-AzContext).Tenant.Id |
| 51 | +``` |
| 52 | + |
| 53 | +## Sign in with a service principal <a name="sp-signin"/> |
| 54 | + |
| 55 | +Service principals are non-interactive Azure accounts. Like other user accounts, their permissions |
| 56 | +are managed with Azure Active Directory. By granting a service principal only the permissions it |
| 57 | +needs, your automation scripts stay secure. |
| 58 | + |
| 59 | +To learn how to create a service principal for use with Azure PowerShell, see |
| 60 | +[Create an Azure service principal with Azure PowerShell](create-azure-service-principal-azureps.md). |
| 61 | + |
| 62 | +To sign in with a service principal, use the `-ServicePrincipal` argument with the |
| 63 | +`Connect-AzAccount` cmdlet. You'll also need the service principal's application ID, sign-in |
| 64 | +credentials, and the tenant ID associate with the service principal. How you sign in with a service |
| 65 | +principal depends on whether it's configured for password-based or certificate-based |
| 66 | +authentication. |
| 67 | + |
| 68 | +### Password-based authentication |
| 69 | + |
| 70 | +Create a service principal to be used in the examples in this section. For more information on |
| 71 | +creating service principals, see |
| 72 | +[Create an Azure service principal with Azure PowerShell](/powershell/azure/create-azure-service-principal-azureps). |
| 73 | + |
| 74 | +```azurepowershell-interactive |
| 75 | +$sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName |
| 76 | +``` |
| 77 | + |
| 78 | +To get the service principal's credentials as the appropriate object, use the |
| 79 | +[Get-Credential](/powershell/module/microsoft.powershell.security/get-credential) cmdlet. This |
| 80 | +cmdlet presents a prompt for a username and password. Use the service principal's `applicationID` |
| 81 | +for the username and convert its `secret` to plain text for the password. |
| 82 | + |
| 83 | +```azurepowershell-interactive |
| 84 | +# Retrieve the plain text password for use with `Get-Credential` in the next command. |
| 85 | +$sp.secret | ConvertFrom-SecureString -AsPlainText |
| 86 | +
|
| 87 | +$pscredential = Get-Credential -UserName $sp.ApplicationId |
| 88 | +Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId |
| 89 | +``` |
| 90 | + |
| 91 | +For automation scenarios, you need to create credentials from a service principal's `applicationId` |
| 92 | +and `secret`: |
| 93 | + |
| 94 | +```azurepowershell-interactive |
| 95 | +$pscredential = New-Object -TypeName System.Management.Automation.PSCredential($sp.ApplicationId, $sp.Secret) |
| 96 | +Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId |
| 97 | +``` |
| 98 | + |
| 99 | +Make sure that you use good password storage practices when automating service principal connections. |
| 100 | + |
| 101 | +### Certificate-based authentication |
| 102 | + |
| 103 | +Certificate-based authentication requires that Azure PowerShell can retrieve information from a |
| 104 | +local certificate store based on a certificate thumbprint. |
| 105 | + |
| 106 | +```azurepowershell-interactive |
| 107 | +Connect-AzAccount -ApplicationId $appId -Tenant $tenantId -CertificateThumbprint <thumbprint> |
| 108 | +``` |
| 109 | + |
| 110 | +When using a service principal instead of a registered application, add the `-ServicePrincipal` argument |
| 111 | +and provide the service principal's Application ID as the `-ApplicationId` parameter's value. |
| 112 | + |
| 113 | +```azurepowershell-interactive |
| 114 | +Connect-AzAccount -ServicePrincipal -ApplicationId $servicePrincipalId -Tenant $tenantId -CertificateThumbprint <thumbprint> |
| 115 | +``` |
| 116 | + |
| 117 | +In PowerShell 5.1, the certificate store can be managed and inspected with the |
| 118 | +[PKI](/powershell/module/pkiclient) module. For PowerShell Core 6.x and later, the process is more |
| 119 | +complicated. The following scripts show you how to import an existing certificate into the |
| 120 | +certificate store accessible by PowerShell. |
| 121 | + |
| 122 | +#### Import a certificate in PowerShell 5.1 |
| 123 | + |
| 124 | +```azurepowershell-interactive |
| 125 | +# Import a PFX |
| 126 | +$credentials = Get-Credential -Message "Provide PFX private key password" |
| 127 | +Import-PfxCertificate -FilePath <path to certificate> -Password $credentials.Password -CertStoreLocation cert:\CurrentUser\My |
| 128 | +``` |
| 129 | + |
| 130 | +#### Import a certificate in PowerShell Core 6.x and later |
| 131 | + |
| 132 | +```azurepowershell-interactive |
| 133 | +# Import a PFX |
| 134 | +$storeName = [System.Security.Cryptography.X509Certificates.StoreName]::My |
| 135 | +$storeLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser |
| 136 | +$store = [System.Security.Cryptography.X509Certificates.X509Store]::new($storeName, $storeLocation) |
| 137 | +$certPath = <path to certificate> |
| 138 | +$credentials = Get-Credential -Message "Provide PFX private key password" |
| 139 | +$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable |
| 140 | +$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($certPath, $credentials.Password, $flag) |
| 141 | +$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) |
| 142 | +$store.Add($Certificate) |
| 143 | +$store.Close() |
| 144 | +``` |
| 145 | + |
| 146 | +## Sign in using a managed identity |
| 147 | + |
| 148 | +Managed identities are a feature of Azure Active Directory. Managed identities are service |
| 149 | +principals assigned to resources that run in Azure. You can use a managed identity service principal |
| 150 | +for sign-in, and acquire an app-only access token to access other resources. Managed identities are |
| 151 | +only available on resources running in an Azure cloud. |
| 152 | + |
| 153 | +This example connects using the managed identity of the host environment. For example, if executed |
| 154 | +on a VirtualMachine with an assigned Managed Service Identity, this allows the code to sign in using |
| 155 | +that assigned identity. |
| 156 | + |
| 157 | +```azurepowershell-interactive |
| 158 | + Connect-AzAccount -Identity |
| 159 | +``` |
| 160 | + |
| 161 | +## Sign in with a non-default tenant or as a Cloud Solution Provider (CSP) |
| 162 | + |
| 163 | +If your account is associated with more than one tenant, sign-in requires the `-Tenant` parameter to |
| 164 | +be specified when connecting. This parameter works with any sign-in method. When logging in, this |
| 165 | +parameter value can either be the Azure object ID of the tenant (Tenant ID) or the fully qualified |
| 166 | +domain name of the tenant. |
| 167 | + |
| 168 | +If you're a [Cloud Solution Provider (CSP)](https://azure.microsoft.com/offers/ms-azr-0145p/), the |
| 169 | +`-Tenant` value **must** be a tenant ID. |
| 170 | + |
| 171 | +```azurepowershell-interactive |
| 172 | +Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx' |
| 173 | +``` |
| 174 | + |
| 175 | +## Sign in to another Cloud |
| 176 | + |
| 177 | +Azure cloud services offer environments compliant with regional data-handling laws. For accounts in |
| 178 | +a regional cloud, set the environment when you sign in with the `-Environment` argument. This |
| 179 | +parameter works with any sign-in method. For example, if your account is in the China cloud: |
| 180 | + |
| 181 | +```azurepowershell-interactive |
| 182 | +Connect-AzAccount -Environment AzureChinaCloud |
| 183 | +``` |
| 184 | + |
| 185 | +The following command gets a list of available environments: |
| 186 | + |
| 187 | +```azurepowershell-interactive |
| 188 | +Get-AzEnvironment | Select-Object -Property Name |
| 189 | +``` |
0 commit comments