How to connect to Microsoft 365 using PowerShell?

Marin Marinov 161 Reputation points
2023-12-13T16:54:36.1566667+00:00

Greetings!

I`m currently learning Microsoft 365 Enterprise administration. I want to perform all the tasks via PowerShell however I am struggling to figure out how to connect to my Microsoft 365 tenant.

I reviewed the Microsoft documentation, researched in Google, asked chatGPT......... at the end I just got confused. Can someone explain me step by step how to connect to Microsoft 365 and create an user?

Thank in advance!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,262 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,446 questions
Microsoft Copilot
Microsoft Copilot
Microsoft terminology for a universal copilot interface.
186 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 33,376 Reputation points Microsoft Vendor
    2023-12-14T05:27:54.2433333+00:00

    Hello,

    We have several modules to manage cloud user identities. All of them need to connect with administrative credentials. Please refer to the below:

    AAD module:

    https://video2.skills-academy.com/en-us/powershell/module/azuread/connect-azuread?view=azureadps-2.0

    https://video2.skills-academy.com/en-us/powershell/module/azuread/new-azureaduser?view=azureadps-2.0

    Msol module:

    https://video2.skills-academy.com/en-us/powershell/module/msonline/connect-msolservice?view=azureadps-1.0

    https://video2.skills-academy.com/en-us/powershell/module/msonline/new-msoluser?view=azureadps-1.0

    MS Graph module:

    https://video2.skills-academy.com/en-us/powershell/module/microsoft.graph.authentication/connect-mggraph?view=graph-powershell-1.0

    https://video2.skills-academy.com/en-us/powershell/module/microsoft.graph.users/new-mguser?view=graph-powershell-1.0

    Please note that both MSOL and Azure AD module will be retiring. You would consider MS Graph module with higher priority.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    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.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Marin Marinov 161 Reputation points
    2023-12-14T16:37:22.0233333+00:00

    Hi Ian, thank you very much for the well structured answer. That`s exactly what I needed! Just in case someone finds the topic in the feature here are the commands:

    #Install the Microsoft Graph PowerShell SDK.
    	Install-Module Microsoft.Graph -Scope AllUsers
     
    #Confirm if the module is installed.
    	Get-InstalledModule
    
    #Invoke Connect-MgGraph before any commands that access Microsoft Graph
    	Connect-MgGraph -Scopes "User.Read.All", "Group.ReadWrite.All"
     
    #Create Microsoft 365 user accounts using MS Graph module.
    
    	$PasswordProfile = @{
        Password = 'xWwvJ]6NMw+bWH-d'
        }
    	 
    	New-MgUser -DisplayName "Jane Doe" -GivenName "Jane" -Surname "Doe" -UserPrincipalName jdoe@testing1466.onmicrosoft.com -UsageLocation "US" -MailNickname "jdoe" -PasswordProfile $PasswordProfile -AccountEnabled
    
    0 comments No comments