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!

Windows for business Windows Server User experience PowerShell
Microsoft Copilot Other
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    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://learn.microsoft.com/en-us/powershell/module/azuread/connect-azuread?view=azureadps-2.0

    https://learn.microsoft.com/en-us/powershell/module/azuread/new-azureaduser?view=azureadps-2.0

    Msol module:

    https://learn.microsoft.com/en-us/powershell/module/msonline/connect-msolservice?view=azureadps-1.0

    https://learn.microsoft.com/en-us/powershell/module/msonline/new-msoluser?view=azureadps-1.0

    MS Graph module:

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

    https://learn.microsoft.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 ******@testing1466.onmicrosoft.com -UsageLocation "US" -MailNickname "jdoe" -PasswordProfile $PasswordProfile -AccountEnabled
    
    0 comments No comments

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.