@Leo Wang , Thanks for the update. I am glad to hear that it is working on windows after we change Visual Studio Code to use PS v5. Congratulations! Here, please let me write a summary for our issue to help others who have the same issue:
Issue Description:
=======================
"Connect-MsGraph' is not recognized as the name of a cmdlet in Visual Studio Code. But it works in Powershell.
Troubleshooting:
======================
1, Try the following steps
A, Right click Visual Studio code and "Run as administrator"
B, Install Powershell and Rest client extension.
C, Run the following commands to install the module and sign in with a global administrator account to connect to Intune graph
Install-Module Microsoft.Graph.Intune
Import-Module Microsoft.Graph.Intune #import intune module
Connect-MSGraph #connect to MS grasp
2, After trying the above steps, we get a new error "Could not load type “system.security.cryptography.SHA256cng” from assembly “system.CORE, version=4.0.0.0, culture=neutral, publickeytoken=“Cbftypingthat’."
3, It seems the class isn't supported by .NET Core and .NET 5, Powershell 7 is built on top of .NET Core and thus can't load the assembly. We'll need to stick to Windows Powershell 5.1 (which uses the .NET 4 runtime).
Resolution:
=====================
After changing Visual Studio Code to use PS v5, it works. To switch the PS version, hold ctrl+shift+P within VS Code, search "PowerShell:Show session menu" and select the PS version.
Meanwhile, I notice you also have issue on MacOS. And it seems the Visual Studio Code can only support PS with version 7 on MacOS. You want to know if there's any method to modify the script to make it working. After doing a lot of research, I did't find any article mention this. However, you can contact the module owner to see if you can get more help:
https://www.powershellgallery.com/packages/Microsoft.Graph.Intune/6.1907.1.0.
Thanks for your time and have a nice day!
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.