title: Get Azure Active Directory Access Token from Powershell
body: <p>I have an Application Id and a Key that I generated in the Azure portal after registering an Azure Active Directory Application.</p>
  
  <p>With this pair and from my Powershell script, I would like to generate an Access Token that I will use in my script in a HTTP Request.</p>
  
  <p>I am able to get an access token using C# (see below code) but I am not sure how to do something similar in Powershell. Is there anything already built in Powershell? If not, can someone point me to a solution? I am pretty sure I am not the only person with this question.</p>
  
  <pre><code>public static async Task&lt;string&gt; GetAccessTokenAsync(string clientId, string appKey, string resourceId)
  {
       string aadInstance = "https://login.microsoftonline.com/{0}";
       string tenant = "&lt;TENANT&gt;.onmicrosoft.com";
       string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
       AuthenticationContext authContext = new AuthenticationContext(authority);
       ClientCredential clientCredential = new ClientCredential(clientId, appKey);
  
       AuthenticationResult authenticationResult = null;       
       authenticationResult = await authContext.AcquireTokenAsync(resourceId, clientCredential);
  
       return authenticationResult.AccessToken;
  }
  </code></pre>