A PowerShell array is a data structure used to store multiple values in a single variable. It allows you to group a collection of items—such as email addresses, usernames, or user IDs—and perform operations on each element collectively. Arrays are especially helpful in Microsoft 365 administration where bulk operations on multiple users or objects are common.
With arrays, you can streamline scripts that apply the same logic (like password resets or license assignments) to a list of users, improving efficiency and consistency.
You can create a PowerShell array using the @() syntax, listing each item inside the parentheses. Once defined, a foreach loop can iterate over the array, applying actions to each item one at a time.
Example:
$items = @("value1", "value2", "value3")
foreach ($item in $items) {
# Do something with $item
}
Arrays are dynamically sized and flexible—you can append new values using += and filter or manipulate them as needed. They make scripts more organized and reusable, especially when working with lists of Microsoft 365 users or groups.
Here’s a script that stores a list of Microsoft 365 user UPNs (whose passwords need to be reset) in an array and resets their passwords using Microsoft Graph PowerShell:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Define an array of user UPNs
$usersToReset = @(
"jackie@7xh7fj.onmicrosoft.com",
"jangri.doe@7xh7fj.onmicrosoft.com",
"jalebi.doe@7xh7fj.onmicrosoft.com"
)
# Set a common temporary password
$tempPassword = "Temp@12345"
# Loop through users and reset their passwords
foreach ($upn in $usersToReset) {
try {
Update-MgUser -UserId $upn -BodyParameter @{
PasswordProfile = @{
Password = $tempPassword
ForceChangePasswordNextSignIn = $true
}
}
Write-Host "✅ Password reset for $upn" -ForegroundColor Green
}
catch {
Write-Host "❌ Failed for $upn - $($_.Exception.Message)" -ForegroundColor Red
}
}
This is a powerful and practical use of arrays to simplify bulk password resets in Microsoft 365 using Graph PowerShell.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex