#Powershell #Microsoft_Graph
## Introduction
Microsoft Graph is a powerful API that connects multiple services within the Microsoft 365 ecosystem. By integrating with Microsoft Graph, developers can access a range of services, such as Microsoft 365, Windows 10, and Enterprise Mobility + Security. One essential aspect of Microsoft Graph is the `mgUser` command, which deals specifically with user-related operations.
This guide provides an overview of the `mgUser` command, explains its various functions, and offers usage examples to help you get started.
## Prerequisites
Before using the `mgUser` command, ensure that you have the following:
- An active Microsoft 365 subscription
- An application registered in the Azure AD portal with appropriate permissions
- The Microsoft Graph PowerShell SDK installed (`Install-Module Microsoft.Graph`)
## Installation
To install the Microsoft Graph PowerShell SDK, open PowerShell and run the following command:
```powershell
Install-Module Microsoft.Graph
```
## Authentication
Authenticate with Microsoft Graph by running:
```powershell
Connect-MgGraph
```
Follow the prompts to sign in.
## Basic mgUser Command Usage
The `mgUser` command deals with user entities in Microsoft 365. Below are common operations you can perform using this command.
### Get a User
Retrieve a user's information using their user ID or user principal name (UPN).
```powershell
Get-MgUser -UserId "
[email protected]"
```
This command returns user details such as display name, mail, and user principal name.
### List All Users
Get a list of all users in the organization.
```powershell
Get-MgUser
```
You can also filter and select specific attributes:
```powershell
Get-MgUser -Select "displayName, mail"
```
### Create a New User
Create a new user in Azure AD with required properties.
```powershell
New-MgUser -DisplayName "John Doe" -UserPrincipalName "
[email protected]" -MailNickname "johndoe" -PasswordProfile @{password="YourPassword123"; forceChangePasswordNextSignIn=$true} -AccountEnabled $true
```
### Update a User
Modify user properties like display name or job title.
```powershell
Update-MgUser -UserId "
[email protected]" -DisplayName "John Doe Updated" -JobTitle "Senior Developer"
```
### Delete a User
Remove a user from your organization.
```powershell
Remove-MgUser -UserId "
[email protected]"
```
### Assigning Licenses
Assigning licenses to a user is essential for providing access to specific services.
```powershell
# Retrieve available licenses
$licenses = Get-MgSubscribedSku
# Assign license to the user
New-MgUserLicenseDetail -UserId "
[email protected]" -AddLicenses @{skuId = $licenses[0].SkuId}
```
## Advanced Usage
### Managing User’s Groups
Get user's group membership:
```powershell
Get-MgUserMemberOf -UserId "
[email protected]"
```
### Retrieving Manager Details
Get a user's manager information:
```powershell
Get-MgUserManager -UserId "
[email protected]"
```
### Retrieve User’s Photo
Fetch the user’s profile photo.
```powershell
Get-MgUserPhoto -UserId "
[email protected]" | Get-MgUserPhotoContent -UserId "
[email protected]"
```
## Remember
always review and update permissions and take appropriate security measures to protect sensitive data. For further details and advanced scenarios, refer to the [official Microsoft Graph documentation](https://docs.microsoft.com/en-us/graph/overview).