Changed to new MGGraph commands.
This commit is contained in:
1 parent
090ec6559a
commit
b7b50dca20
1 file changed
+46
@@ -0,0 +1,46 @@
|
||||
# Define the local path to your default profile picture
|
||||
$PicturePath = "C:\Temp\Profile_Picture.png"
|
||||
|
||||
if (-not (Test-Path $PicturePath)) {
|
||||
Write-Error "The specified picture file was not found at $PicturePath"
|
||||
exit
|
||||
}
|
||||
|
||||
# Connect to Microsoft Graph. You must have the Microsoft.Graph.Users module installed.
|
||||
# Scopes needed for modifying user photos.
|
||||
Connect-MgGraph -Scopes "User.ReadWrite.All"
|
||||
|
||||
# Retrieve standard member accounts, ignoring service accounts or guests
|
||||
$Users = Get-MgUser -Filter "userType eq 'Member' and accountEnabled eq true" -All
|
||||
|
||||
foreach ($User in $Users) {
|
||||
$HasPhoto = $true
|
||||
|
||||
try {
|
||||
# Attempt to retrieve the user's photo.
|
||||
# If none exists, Graph throws an ImageNotFound exception.
|
||||
$null = Get-MgUserPhoto -UserId $User.Id -ErrorAction Stop
|
||||
}
|
||||
catch {
|
||||
$HasPhoto = $false
|
||||
}
|
||||
|
||||
if (-not $HasPhoto) {
|
||||
Write-Host "No profile picture found for $($User.UserPrincipalName). Applying default..." -ForegroundColor Cyan
|
||||
|
||||
try {
|
||||
# Set the photo using the local file
|
||||
Set-MgUserPhotoContent -UserId $User.Id -InFile $PicturePath -ErrorAction Stop
|
||||
Write-Host "Successfully applied picture to $($User.UserPrincipalName)." -ForegroundColor Green
|
||||
}
|
||||
catch {
|
||||
Write-Host "Failed to apply picture to $($User.UserPrincipalName). Error: $($_.Exception.Message)" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "Profile picture already exists for $($User.UserPrincipalName). Skipping." -ForegroundColor DarkGray
|
||||
}
|
||||
}
|
||||
|
||||
# Always sever the session.
|
||||
Disconnect-MgGraph
|
||||
Reference in new issue
Block a user