﻿# 如何使用 PowerShell 批次管理 Azure AD 使用者資訊

微軟真的家大業大，用 PowerShell 管理 Azure AD 使用者資訊竟然已經累積出了 4 套完全不一樣的 Cmdlets 模組，其中最建議使用的 Microsoft Graph PowerShell 真的是無敵難用到爆，但安全性高、速度快是其優點。本篇文章我就整理一下我近期常用的一些 Cmdlet 命令，方便日後透過 PowerShell 管理公司內部的使用者資訊。

### 登入

```powershell
# 先設定登入的帳號密碼
$Credential = Get-Credential -Message "Login" -UserName "user@example.com"

# 連接 Azure Active Directory PowerShell for Graph
# Import-Module AzureAD
Connect-AzureAD -Credential $Credential

# 連接 MSOnline V1 PowerShell module for Azure Active Directory
# Install-Module MSOnline
# Import-Module MSOnline
Connect-MsolService -Credential $Credential

# 連接 Exchange Online PowerShell V3
# Install-Module -Name ExchangeOnlineManagement
# Import-Module -Name ExchangeOnlineManagement
Connect-ExchangeOnline -Credential $Credential

# 連接 Microsoft Graph PowerShell
# Install-Module -Name Microsoft.Graph
Connect-MgGraph -TenantId "$TenantId"
```

### MSOnline V1 PowerShell module for Azure Active Directory

-   取得所有使用者
    
    ```powershell
    $users = Get-MsolUser -All
    ```
    
-   取得特定部門下所有使用者
    
    ```powershell
    $users = Get-MsolUser -All | Where-Object { $_.Department -eq "專案研發一部" }
    ```
    
    ```powershell
    $users = Get-MsolUser -All | Where-Object { $_.Department -eq "專案研發一部" -or $_.Department -eq "專案研發二部" -or $_.Department -eq "營運管理部" }
    ```
    
-   取得不在特定部門下所有使用者
    
    ```powershell
    $users = Get-MsolUser -All | Where-Object { $_.Department -ne "專案研發一部" -and $_.Department -ne "專案研發二部" -and $_.Department -ne "營運管理部" }
    ```
    
-   將使用者匯出成 CSV 檔
    
    ```powershell
    $users | select DisplayName,UserPrincipalName,Department | Sort-Object { $_.Department } | Export-Csv -Path "g:\export.csv" -NoTypeInformation -NoClobber -Encoding utf8
    ```
    
-   建立 M365 帳號並指派授權
    
    ```powershell
    New-MsolUser -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment -MobilePhone $MobilePhone -Office $Office -PhoneNumber $PhoneNumber -PreferredLanguage $PreferredLanguage -State $State -StreetAddress $StreetAddress -Title $Title -AlternateEmailAddresses $AlternateEmailAddresses -Country $Country -PostalCode $PostalCode -City $City -Department $Department -Fax $Fax
    ```
    
-   重設使用者密碼
    
    ```powershell
    Set-MsolUserPassword -UserPrincipalName "$UserPrincipalName" -NewPassword "$PlainPassword" -ForceChangePassword $true
    ```
    
-   設定使用者顯示名稱
    
    ```powershell
    Set-MsolUser -UserPrincipalName "$UserPrincipalName" -DisplayName ("$LastName" + "$FirstName" + "$EngName")
    ```
    
-   設定使用者所屬部門
    
    ```powershell
    Set-MsolUser -UserPrincipalName "user@example.com" -Department "專案研發二部"
    ```
    

### Azure Active Directory PowerShell for Graph

-   取得所有使用者
    
    ```powershell
    $users = Get-AzureADUser -All $true
    ```
    
-   取得特定部門下所有使用者
    
    ```powershell
    $users = Get-AzureADUser -Filter "department eq '專案研發一部'"
    ```
    
    ```powershell
    $users = Get-AzureADUser -Filter "department eq '專案研發一部' or department eq '專案研發二部' or department eq '營運管理部'"
    ```
    
-   取得員工的直屬主管名稱
    
    ```powershell
    $users | foreach {
      $employee = $_
      $manager = Get-AzureADUserManager -ObjectId $_.ObjectId
      $employee.DisplayName + "`t主管是 " + $manager.DisplayName
    }
    ```
    

### Microsoft Graph PowerShell

-   找出特定 Cmdlet 需要哪些 OAuth 2.0 Scope 權限
    
    ```powershell
    Find-MgGraphCommand -command "Get-MgUser" | Select -First 1 -ExpandProperty Permissions
    ```
    
-   取得所有使用者清單
    
    ```powershell
    Connect-MgGraph -Scopes 'User.Read.All'
    $users = Get-MgUser -All
    ```
    
-   列出所有使用者基本資訊（含信箱地址）
    
    ```powershell
    $users | Format-List  ID, DisplayName, Mail, UserPrincipalName
    ```
    

### Exchange Online PowerShell V3

-   設定直屬主管
    
    ```powershell
    $User = "tom"
    $Manager = "will"
    Set-User "$UserName" -Company "$CompanyName" -Manager "$Manager" -Confirm:$false
    ```
    
-   設定中斷 Exchange Online PowerShell 的 Session
    
    ```powershell
    Disconnect-ExchangeOnline -Confirm:$false
    ```
    

### 相關連結

-   MSOnline V1 PowerShell module for Azure Active Directory
    
    -   [MSOnline Module | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/microsoftgraph/migration-steps)
-   Azure Active Directory V2 PowerShell
    
    -   [Azure Active Directory PowerShell for Graph](https://learn.microsoft.com/en-us/powershell/azure/active-directory/overview?view=azureadps-2.0&WT.mc_id=DT-MVP-4015686)
        
        注意：Azure AD Powershell 將在 2024/03/30 退役！
        
    -   [AzureAD Module | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/azuread/?view=azureadps-2.0&preserve-view=true&WT.mc_id=DT-MVP-4015686)
        
        -   [Get-AzureADUser](https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureaduser?view=azureadps-2.0&WT.mc_id=DT-MVP-4015686)
-   Microsoft Graph PowerShell v1.0
    
    -   [Microsoft Graph PowerShell overview](https://learn.microsoft.com/en-us/powershell/microsoftgraph/overview?view=graph-powershell-1.0&WT.mc_id=DT-MVP-4015686)
    -   [Get started with the Microsoft Graph PowerShell SDK](https://learn.microsoft.com/en-us/powershell/microsoftgraph/get-started?view=graph-powershell-1.0&WT.mc_id=DT-MVP-4015686)
        -   [Get-MgUser](https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users/get-mguser?view=graph-powershell-1.0&WT.mc_id=DT-MVP-4015686)
-   Exchange Online PowerShell V3 (EXO V3)
    
    -   [關於 Exchange Online PowerShell 模組](https://learn.microsoft.com/zh-tw/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps&WT.mc_id=DT-MVP-4015686)
        -   [安裝和維護 Exchange Online PowerShell 模組](https://learn.microsoft.com/zh-tw/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps&WT.mc_id=DT-MVP-4015686#install-and-maintain-the-exchange-online-powershell-module)
