Back to script library
Entra / Microsoft 365 · Teams

Convert Teams background files for v2

Converts custom Teams V1 background images so they meet Teams v2 client size requirements, using local image resize helpers.

Connect & set up

Run these once per session. All scopes are read-only unless the script makes changes.

# Review required modules and connection steps before running.
# Connect to Microsoft Graph or Exchange Online as needed for this script.

Run it

The main script. Copy it, or download the .ps1 and run it from your console.

Function Resize-Image() {
[CmdLetBinding(
SupportsShouldProcess=$true,
PositionalBinding=$false,
ConfirmImpact="Medium",
DefaultParameterSetName="Absolute"
)]
Param (
[Parameter(Mandatory=$True)]
[ValidateScript({
$_ | ForEach-Object {
Test-Path $_
}
})][String[]]$ImagePath,
[Parameter(Mandatory=$False)][Switch]$MaintainRatio,
[Parameter(Mandatory=$False, ParameterSetName="Absolute")][Int]$Height,
[Parameter(Mandatory=$False, ParameterSetName="Absolute")][Int]$Width,
[Parameter(Mandatory=$False, ParameterSetName="Percent")][Double]$Percentage,
[Parameter(Mandatory=$False)][System.Drawing.Drawing2D.SmoothingMode]$SmoothingMode = "HighQuality",
[Parameter(Mandatory=$False)][System.Drawing.Drawing2D.InterpolationMode]$InterpolationMode = "HighQualityBicubic",
[Parameter(Mandatory=$False)][System.Drawing.Drawing2D.PixelOffsetMode]$PixelOffsetMode = "HighQuality",
[Parameter(Mandatory=$False)][String]$NameModifier = "resized"
)
Begin {
If ($Width -and $Height -and $MaintainRatio) {
Throw "Absolute Width and Height cannot be given with the MaintainRatio parameter."
}
If (($Width -xor $Height) -and (-not $MaintainRatio)) {
Throw "MaintainRatio must be set with incomplete size parameters (Missing height or width without MaintainRatio)"
}
If ($Percentage -and $MaintainRatio) {
Write-Warning "The MaintainRatio flag while using the Percentage parameter does nothing"
}
}
Process {
Add-Type -AssemblyName 'System.Drawing'
ForEach ($Image in $ImagePath) {
$Path = (Resolve-Path $Image).Path
$Dot = $Path.LastIndexOf(".")
#Add name modifier (OriginalName_{$NameModifier}.jpg)
$OutputPath = $Path.Substring(0,$Dot) + "_" + $NameModifier + $Path.Substring($Dot,$Path.Length - $Dot)
$OldImage = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Path
# Grab these for use in calculations below.
$OldHeight = $OldImage.Height
$OldWidth = $OldImage.Width
If ($MaintainRatio) {
$OldHeight = $OldImage.Height
$OldWidth = $OldImage.Width
If ($Height) {
$Width = $OldWidth / $OldHeight * $Height
}
If ($Width) {
$Height = $OldHeight / $OldWidth * $Width
}
}
If ($Percentage) {
$Product = ($Percentage / 100)
$Height = $OldHeight * $Product
$Width = $OldWidth * $Product
}
$Bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Width, $Height
$NewImage = [System.Drawing.Graphics]::FromImage($Bitmap)
#Retrieving the best quality possible
$NewImage.SmoothingMode = $SmoothingMode
$NewImage.InterpolationMode = $InterpolationMode
$NewImage.PixelOffsetMode = $PixelOffsetMode
$NewImage.DrawImage($OldImage, $(New-Object -TypeName System.Drawing.Rectangle -ArgumentList 0, 0, $Width, $Height))
If ($PSCmdlet.ShouldProcess("Resized image based on $Path", "save to $OutputPath")) {
$Bitmap.Save($OutputPath)
}
$Bitmap.Dispose()
$NewImage.Dispose()
$OldImage.Dispose()
}
}
}
# Start of processing to convert Teams background images
# First, identify the target directory where Teams V2 stores its background images. Each
# image has two files - a full version and a thumbnail.
$TargetDirectory = ($Env:appdata -Split("\\Roaming"))[0] + "\Local\Packages\MSTeams_8wekyb3d8bbwe\LocalCache\Microsoft\MSTeams\Backgrounds\Uploads"
# Now find the folder where Teams V1 stored its background files
$TeamsV1Directory = $Env:appdata + "\Microsoft\Teams\Backgrounds\Uploads"
# Check that the two folders are available
If (!(Test-Path -Path $TargetDirectory)) {
New-Item -Path $TargetDirectory -ItemType Directory
}
If (!(Test-Path $TeamsV1Directory)) {
Write-Host ("The Teams V1 folder for background images is not on this PC - exiting!" -f $TeamsV1Directory)
break
}
# Get the set of files that we can convert
Set-Location -Path $TeamsV1Directory
[array]$Files = Get-ChildItem "*.jpg" | Select-Object -ExpandProperty Name
[int]$i =0
# If we can find some JPG files to convert, go ahead and create new files
If ($Files) {
# Remove thumb nails and only process full definition files
$Files = $Files | Where-Object {$_ -notlike "*_thumb.jpg*"}
ForEach ($File in $Files) {
Write-Host ("Processing {0}..." -f $File)
$Name = $File.Split(".")[0]
$OutputFile = $Name + "_resized.jpg"
# Create 1920x1080 version of file
Resize-image -ImagePath $File -height 1080 -Width 1920
If ($OutputFile) {
$Guid = New-Guid
$FullFileName = ("{0}.jpg" -f $Guid)
Rename-Item $OutputFile $FullFileName
}
# Now create the thumbnail image
$OutputFile = $Name + "_resized.jpg"
Resize-image -ImagePath $File -height 158 -Width 220
If ($OutputFile) {
$ThumbFileName = ("{0}_thumb.jpg" -f $Guid)
Rename-Item $OutputFile $ThumbFileName
}
$CopyFiles = ("{0}*.jpg" -f $Guid)
# Copy files to the Teams V2 folder and clean up by removing them from the V1 folder
Copy-Item $CopyFiles $TargetDirectory
Remove-Item $CopyFiles
$i++
} # End ForEach Files
} # End if Files
Write-Host ("All done. {0} files processed and copied to the Teams V2 folder. You should be able to use these files as background images in Teams V2 meetings" -f $i)
Set-Location -Path $TargetDirectory
Get-ChildItem "*.jpg"
Set-Location -Path "C:\"
Attribution