Automating Software Deployment in Microsoft 365 with PowerShell

Efficient software deployment is essential for ensuring that users have access to the applications they need to perform their jobs. This PowerShell script automates the process of deploying software packages across Microsoft 365 environments, making it easier for administrators to manage application distributions.
This script will:
1. Connect to Microsoft Graph API.
2. Retrieve a list of users to whom the software will be deployed.
3. Deploy specified software applications to the selected users.
By utilizing this script, IT administrators can streamline software deployment processes, ensuring all users have the necessary tools to be productive.

# Install the Microsoft.Graph module if not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
    Install-Module -Name Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'Application.ReadWrite.All', 'User.ReadWrite.All'
# Specify the application ID and users to deploy to
$appId = "your-app-id"  # Replace with the application ID to deploy
$users = @("[email protected]", "[email protected]")  # List of users
# Loop through each user and deploy the application
foreach ($user in $users) {
    try {
        New-MgUserAppRoleAssignment -UserId $user -AppRoleId $appId -ResourceId "your-resource-id"  # Replace with the resource ID
        Write-Host "Successfully deployed application to $user."
    } catch {
        Write-Host "Failed to deploy application to $user. Error: $_"
    }
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Software deployment process completed."