Retrieving all cmdlets and help URLs from the new Microsoft Graph API modules

Things are changing… The Azure AD and MSOL modules will be getting a deprecated status, this was initially set for June 2022 and now that has been postponed until after December 2022. You need to start updating scripts, be prepared for that. In this blog post, I wanted to show you a way to collect all the new cmdlets and show their description, synopsis (A brief summary), and the help URL for more information.

Example of changes

The table below is an example of how the naming of the cmdlets will change for Azure AD cmdlets, there’s always an ‘Mg’ added to let you know it’s a Microsoft Graph cmdlet:

Azure AD cmdletsMicrosoft Graph PowerShell cmdlets
Add-AzureADDeviceRegisteredOwnerNew-MgDeviceRegisteredOwnerByRef
Add-AzureADDeviceRegisteredUserNew-MgDeviceRegisteredUserByRef
Get-AzureADDeviceGet-MgDevice
Get-AzureADDeviceConfigurationGet-MgDeviceManagementDeviceConfiguration
Get-AzureADDeviceRegisteredOwnerGet-MgDeviceRegisteredOwner
Get-AzureADDeviceRegisteredUserGet-MgDeviceRegisteredUser
New-AzureADDeviceNew-MgDevice
Remove-AzureADDeviceRemove-MgDevice
Remove-AzureADDeviceRegisteredOwner
Remove-AzureADDeviceRegisteredUser
Set-AzureADDeviceUpdate-MgDevice
https://docs.microsoft.com/en-us/powershell/microsoftgraph/azuread-msoline-cmdlet-map?view=graph-powershell-beta#devices

Requirements for the script

As said, the script should retrieve all the new cmdlets and get the description and synopsis of them. Also, a link to the help page on docs.microsocft.com should be there so that you can easily find information about the new cmdlet.

It should also install all the new Microsoft.Graph module if they are not present already, this way you will have everything ready for changing your scripts. And it should also remove any older versions so that only the current version is available for use.

After collecting all the information, the data should be saved to a CSV file which you can use to find more information about the cmdlets.

Console output

When running the script, you should see the status while it’s installing the new modules if needed and collecting the information about the new cmdlets. This looks like this:

The CSV-file

After the script ends, you can browse to the location you specified in the $csvlocation variable and open the CSV file. It contains about 5K lines at this moment and looks like this screenshot below:

The script

Below are the contents of the script, it has a lot of formatting parts in it. Not all information has been added to the modules yet and sometimes there is no synopsis or help URL available, also it sometimes refers to a cmdlet name without ‘Mg’ so sadly it isn’t always consistent yet. It does take about 15 minutes sometimes to get all the information and has an appetite for memory πŸ˜€

#Set CSV location
$csvlocation = 'd:\temp\Microsoft.Graph.Cmdlets.csv'
 
#Get a list of all available Microsoft.Graph modules
Write-host ("Getting a list of available online Microsoft.Graph modules...") -ForegroundColor Green
$OnlineMicrosoftGraphModules = find-module -name Microsoft.Graph* | Where-Object Name -NotMatch 'Microsoft.Graph.PlusPlus' | Sort-Object Name
 
#Get a list of all installed Microsoft.Graph Modules
Write-host ("Getting a list of installed Microsoft.Graph modules...") -ForegroundColor Green
$InstalledMicrosoftGraphModules = Get-InstalledModule -Name Microsoft.Graph*
 
#Install and import all Microsoft.Graph modules except the PlusPlus module which is for AzureAD 'work or school' accounts and 'personal' Microsoft accounts
Write-Host ("Installing all Microsoft.Graph Modules but skipping is already installed...") -ForegroundColor Green
foreach ($module in $OnlineMicrosoftGraphModules) {
    if (-not ($InstalledMicrosoftGraphModules -match $module.Name)) {
        write-host ("Installing {0})..." -f $module.Name)-ForegroundColor Green
        Install-Module -Name $module.Name -ErrorAction SilentlyContinue
    }
}
 
#Resfresh the list of all installed Microsoft.Graph Modules after installing all Microsft Graph modules
Write-host ("Resfreshing the list of installed Microsoft.Graph modules...") -ForegroundColor Green
$InstalledMicrosoftGraphModules = Get-InstalledModule -Name Microsoft.Graph*
 
#Remove oldest version of Microsoft.Graph modules if there are more versions installed
Foreach ($Module in $InstalledMicrosoftGraphModules | Sort-Object Name) {
    Write-Host ("Checking for older versions of the {0} PowerShell Module and removing older versions if found..." -f $Module.Name) -ForegroundColor Green
    $AllVersions = Get-InstalledModule -Name $Module.Name -ErrorAction:SilentlyContinue | Sort-Object PublishedDate -Descending
    $MostRecentVersion = $AllVersions[0].Version
    if ($AllVersions.Count -gt 1 ) {
        Foreach ($Version in $AllVersions) {
            if ($Version.Version -ne $MostRecentVersion) {
                Write-Host ("Uninstalling previous version {0} of Module {1}" -f $Version.Version, $Module.Name) -ForegroundColor Yellow
                Uninstall-Module -Name $Module.Name -RequiredVersion $Version.Version -Force:$True
            }
        }
    }
}
 
#retrieve all cmdlets together with the synopsis and add them to $total
$total = foreach ($module in $InstalledMicrosoftGraphModules) { 
    Write-Host ("Processing {0}..." -f $module.Name) -ForegroundColor Green
    $cmdlets = get-command -Module $module.Name
    foreach ($cmdlet in $cmdlets) {
        #Retrieve Synopsis (Remove Read-Only, Read-Wite, Nullable and Supports $expand if found) and URL to docs.microsoft.com for the cmdlet
        $help = Get-Help $cmdlet
        $synopsis = $help.Synopsis.replace('Read-only.', '').replace('Read-Write.', '').replace('Nullable.', '').replace('Supports $expand.', '').replace('Not nullable.', '').replace('\r', " ")
        $synopsis = $synopsis -replace '\n', ' ' -creplace '(?m)^\s*\r?\n', ''
        #Set variable for non matching cmdlet name and synopsis content
        $cmdletoldname = $cmdlet.Name.Replace('-', '-Mg')
        $url = $help.relatedLinks.navigationLink.uri
        #Set Synopsis or URL to "Not Available" when no data is found
        if ($null -eq $synopsis -or $synopsis.Length -le 2 -or $synopsis -match $cmdletoldname -or $synopsis -match $cmdlet.Name) { $synopsis = "Not available" }
        if ($null -eq $url) { $url = "Not available" }
        [PSCustomObject]@{
            Source   = $cmdlet.Source
            Version  = $cmdlet.Version
            Cmdlet   = $cmdlet.Name
            Synopsis = $synopsis
            URL      = $url
        }
    }
}
 
#Save all results to the CSV location specified in the variable $CSVlocation
Write-Host ("Exporting results to {0}" -f $csvlocation) -ForegroundColor Green
try {
    $total | Sort-Object Source, Cmdlet | Export-Csv -Path $csvlocation -NoTypeInformation -Delimiter ';' -Encoding UTF8
}
catch {
    Write-Warning ("Error saving results to {0}, please check if path is accessible" -f $csvlocation)
}

Download the script(s) from GitHub here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.