Skip to content

PowerShell support

Bogdan Gavril edited this page Feb 12, 2020 · 3 revisions

How to use MSAL form PowerShell

There is no Microsoft supported PoswerShell module or wrapper for MSAL libraries. However, PowerShell was designed to be able to call into .NET code and there are numerous resources that describe how to do this.

Make sure you load the correct DLL

After you download the MSAL nuget package, unzip it, and take a look inside. In the lib folder there are the DLLs you are looking for:

If you are writing modules for the new PowerShell Core, then you should load the netcoreapp2.1 version. If you are writing a module for PowerShell classic, then look into the net45 directory. If you aren't sure, start with the net45 version, which only works on Windows.

Avoid loading the netstandard1.3 DLL, as this version is missing a lot of functionality.

Don't forget about token caching

MSAL.NET will create and manage a token cache, but it will NOT persist it. You are responsible for persisting and encrypting the token cache. If you do not, MSAL will only keep the token cache in memory, and when the process stops, the tokens are lost, and users will have to relogin.

Windows

On Windows, all our samples use DPAPI to encrypt a file with the token cache. Inspect this sample for details.

Mac and Linux

If you target PowerShell Core / .NET Core, it important to understand the DPAPI encryption solution above will NOT work. For a cross platform token cache persistence implementation, have a look at:

https://github.com/AzureAD/microsoft-authentication-extensions-for-dotnet

Getting started with MSAL.NET

Acquiring tokens

Web Apps / Web APIs / daemon apps

Desktop/Mobile apps

Advanced topics

FAQ

Other resources

Clone this wiki locally