"Connect-MsGraph' is not recognized as the name of a cmdlet

Leo Wang 6 Reputation points
2022-06-08T03:41:16.647+00:00

Dear Forum Fam,

I have a few PS scripts, which work fine with Windows PowerShell.

But as soon as I try to run them with VS Code, I get the error "Connect-MsGraph' is not recognized as the name of a cmdlet".

Both Windows PowerShell and VS Code got the same Intune modules installed.

Anything I missed here setting up VS Code?

Thanks.

Windows for business Windows Server User experience PowerShell
Microsoft Security Intune Other
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Crystal-MSFT 53,981 Reputation points Microsoft External Staff
    2022-06-08T07:22:28.687+00:00

    @Leo Wang , Based on my test, here are steps I tried what are working.

    1. Right click Visual Studio code and "Run as administrator"
    2. Install Powershell and Rest client extension
      209369-image.png
    3. 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

    209391-image.png

    Hope it can help.


    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.

    1 person found this answer helpful.

  2. EnterpriseArchitect 6,041 Reputation points
    2022-06-08T04:07:53.38+00:00

    @Leo Wang , please try to execute this line:

    > Install-Module Microsoft.Graph -Scope AllUsers   
    > Connect-MgGraph -TenantId "yourTenant GUID"  
    

  3. Leo Wang 6 Reputation points
    2022-06-14T11:33:17.957+00:00

    Hi @Crystal-MSFT ,

    I am getting the error straight away as soon as I ran the script, the Azure signin did not even come up.

    Could not load type “system.security.cryptography.SHA256cng” from assembly “system.CORE, version=4.0.0.0, culture=neutral, publickeytoken=“Cbftypingthat’.


  4. Crystal-MSFT 53,981 Reputation points Microsoft External Staff
    2022-06-15T08:30:59.687+00:00

    @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:
    211607-image.png
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.