# How to find and replace commands from AzureAD and MSOnline deprecated modules in your scripts for the Graph ones

If you are using PowerShell modules ***AzureAD***, ***AzureADPreview***, or ***MSOnline*** in your scripts you should be aware that they are gonna be [retired on 30.6.2023](https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/whats-deprecated-azure-ad). Moreover licensing MSOL cmdlets will **stop working** on 31.3.2023! So now is a good time to start replacing these modules in your scripts and the modules you are using.

What you should use from now on is [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-powershell).

---

# TL;DR

Use functions `Get-ModuleCommandUsedInCode` and `Get-CorrespondingGraphCommand` (from my module [DependencySearch](https://www.powershellgallery.com/packages/DependencySearch)) like this 👇

```plaintext
Install-Module DependencySearch

$scriptsDir = "<pathToFolderWithYourPS1Scripts>"

# get ps1 scripts
$scripts = Get-ChildItem $scriptsDir -Recurse -File | ? Extension -in ".ps1", ".psm1" | select -exp FullName

# load modules whose cmdlets should be searched
$deprecatedModule = @()
"AzureAD", "AzureADPreview", "MSOnline", "AzureRM" | % {
    $module = Get-Module $_ -ListAvailable
    if ($module) {
        $deprecatedModule += $module
    } else {
        Write-Warning "Module $_ isn't available on your computer"
    }
}

# output used cmdlets and corresponding Graph API cmdlet if available
$scripts | % {
    Get-ModuleCommandUsedInCode -scriptPath $_ -module $deprecatedModule | select *, @{n = 'GraphCommand'; e = { (Get-CorrespondingGraphCommand $_.command).GraphCommand } } | select * -exclude module | ft -AutoSize
}
```

And the result will look like this 👇 a.k.a. on the left side you'll see commands that need to be replaced and on the right side by which Graph command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675944065873/1a54e8d3-2c71-4a4a-8bd3-1be0429335cb.png align="center")

---

# Find commands from the deprecated module(s) in my scripts

For identifying scripts where commands from given (deprecated in our case) modules are used I've created a function `Get-ModuleCommandUsedInCode` (in module [DependencySearch](https://www.powershellgallery.com/packages/DependencySearch))

The function requires two parameters:

* script path (where commands will be searched)
    
* module(s) object (retrieved using `Get-Module`)
    

Function:

* gets the name of cmdlets, functions and aliases defined in given module(s)
    
* gets all used commands in a given file (using AST) and outputs just those defined in given module(s)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675945136606/52e9f840-f8c6-4d47-8a4f-ce6f23984bc7.png align="center")

---

# Find the corresponding Graph command to the deprecated one

Now when we are able to extract deprecated commands using `Get-ModuleCommandUsedInCode` we need a way to "translate" such commands to their new Graph API version.

There is an official page with a "[translation map](https://learn.microsoft.com/en-au/powershell/microsoftgraph/azuread-msoline-cmdlet-map?view=graph-powershell-beta)" so I've created a function `Get-CorrespondingGraphCommand` that:

* extracts HTML tables from this page (using `ConvertFrom-HTMLTable` function from CommonStuff module)
    
* caches the result object to your user profile
    
* uses such information to get the corresponding Graph API command
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675945481642/907eaa84-d19f-4e75-8fcc-b611db3f3903.png align="left")

---

# Summary

Thanks to functions `Get-ModuleCommandUsedInCode` and `Get-CorrespondingGraphCommand` that were explained in previous sections you are now able to easily identify all your scripts where those deprecated modules are used, moreover, you know which cmdlet from [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-powershell) should be used instead (in case there is some 😁).

Hopefully, that will make your migration journey easier 🙂
