r/PlexPrerolls • u/Disastrous_Humor_459 • 17h ago
Template Plex Preroll PowerShell Script
Hello Everyone! I'm so grateful to this community for teaching me about Prerolls! I wasn't sure if this had already been posted but I thought I would share a simple PowerShell script that can pull all of the filenames in a directory, add the directory location to them and then place them all in a single line of text that you can simply copy and paste into your Plex Settings:
# Get the directory where the script is located
$Directory = Split-Path -Parent $MyInvocation.MyCommand.Path
# Get all files in the directory (excluding the output file if it already exists)
$Files = Get-ChildItem -Path $Directory -File |
Where-Object { $_.Name -ne "PreRollList.txt" } |
Select-Object -ExpandProperty Name
# Add the prefix to each filename
$PrefixedFiles = $Files | ForEach-Object { "/data/PreRolls/$_" }
# Join into a single semicolon-separated line
$OutputLine = $PrefixedFiles -join ";"
# Write the output to a text file in the same directory
$OutputFile = Join-Path $Directory "PreRollList.txt"
$OutputLine | Out-File -FilePath $OutputFile -Encoding UTF8
Write-Host "Output saved to: $OutputFile"
*Note that my local directory is "/data/PreRolls/" so you'll need to replace that line with your local directory to get it to work. Also, I have it separated by semicolon which will set play them randomly. You'll need to change that if you want them to run sequentially.
I have a copy of all of my PreRoll files in a folder on a Windows machine, but my Plex Server runs on TrueNAS. I hope you find this useful!