How to use the AzureAD module in PowerShell Core
Whilst some of the Azure Active Directory PowerShell for Graph module (AzureAD
) functionality has been rolled into the new Azure PowerShell Az module (Az
), it's not currently (and might never be) a replacement for the full power of what you can achieve with AzureAD
.
So, there's every chance you'll find yourself needing to use both side-by-side, which is absolutely fine until you want to do that using the new cross-platform PowerShell Core.
At time of writing there's no publicly available version of AzureAD
that works with PowerShell Core - however, this post will explain that it is indeed possible to use this module functionality, as well as detailing the steps needed to get it working.
1. Register the Posh Test Gallery package source
Whilst there's no PowerShell Core supported version of AzureAD
in the central PowerShell Gallery, there is a private preview version available in the Posh Test Gallery. Whilst this is indeed an "internal" gallery, for testing purposes, this preview version of the module is already in use in the Azure Cloud Shell, so it's a fairly safe bet that a) it's working as you expect and b) it will eventually move out of preview status.
If you haven't used the Posh Test Gallery before, you'll need to register it as an additional package source in your PowerShell Core environment.
# Check if test gallery is registered
$packageSource = Get-PackageSource -Name 'Posh Test Gallery'
if (!$packageSource)
{
$packageSource = Register-PackageSource -Trusted -ProviderName 'PowerShellGet' -Name 'Posh Test Gallery' -Location 'https://www.poshtestgallery.com/api/v2/'
}
2. Install the AzureAD.Standard.Preview module
Now that the gallery is registered, you can install the preview version of the AzureAD
module - which is called AzureAD.Standard.Preview
- just as you would with any regular, non-preview module. PowerShell Core will automatically check the Posh Test Gallery as well for this module when you call the Install-Module
cmdlet.
# Check if module is installed
$module = Get-Module 'AzureAD.Standard.Preview' -ListAvailable -ErrorAction SilentlyContinue
if (!$module)
{
Write-Host "Installing module AzureAD.Standard.Preview ..."
$module = Install-Module -Name 'AzureAD.Standard.Preview' -Force -Scope CurrentUser -SkipPublisherCheck -AllowClobber
Write-Host "Module installed"
}
3. Import the installed module
Now that the module is installed, you need to explicitly import it into your current session. You shouldn't need to do this, as typically a module is automatically imported after install, however, in this case it doesn't seem to happen as expected. The easiest way to do this is to use the RootModule
property of the installed module which points to the AzureAD.Standard.Preview.psm1
file in whatever local file path your installation points to (in my case C:\Users\JamesBroome\Documents\PowerShell\Modules\AzureAD.Standard.Preview\0.1.599.7
)
# Module doesn't automatically load after install - need to import explictly for Pwsh Core
Import-Module $module.RootModule
4. Use the module!
That's it - now that the module is installed and imported you should be able to use it in your PowerShell Core session exactly as you'd expect. Here's the full source code as a reusable PowerShell function:
function Install-AzureADStandard()
{
# Check if test gallery is registered
$packageSource = Get-PackageSource -Name 'Posh Test Gallery'
if (!$packageSource)
{
$packageSource = Register-PackageSource -Trusted -ProviderName 'PowerShellGet' -Name 'Posh Test Gallery' -Location 'https://www.poshtestgallery.com/api/v2/'
}
# Check if module is installed
$module = Get-Module 'AzureAD.Standard.Preview' -ListAvailable -ErrorAction SilentlyContinue
if (!$module)
{
Write-Host "Installing module AzureAD.Standard.Preview ..."
$module = Install-Module -Name 'AzureAD.Standard.Preview' -Force -Scope CurrentUser -SkipPublisherCheck -AllowClobber
Write-Host "Module installed"
}
# Module doesn't automatically load after install - need to import explictly for Pwsh Core
Import-Module $module.RootModule
}
Install-AzureADStandard
Conclusion
This post has shown that whilst no public version of the AzureAD
module is compatible with PowerShell Core, it's pretty easy to install and use the preview version of AzureAD.Standard.Preview
module from the Posh Test Gallery in order to retain that functionality in your PowerShell Core scripts.
Hopefully an official version will become available soon, but in the meantime, the steps above will allow you to continue using AzureAD
functionality.