Use PowerShell to Update your Plex Library with new content

This is a script that will allow you to update your Plex library via PowerShell.  It saves you having to go into Plex for Web to update libraries and can be used in conjunction with other scripts.

In order to use this script you will need to first get your Plex token. You can do this by doing the following:

1. Open Plex for Web and go to Movies

2. Select any movie and click on the three dots to take you to the context menu.

3. Select Get Info

4. Click View XML

5. Copy exverything from X-Plex-Token= to the end in the address bar

6. Insert this into the $Token field in the script

Once you run the script you will be offered a choice of which library you want to update.

 

$Token = ?X-Plex-Token=EuFbuGyDDnpHnZHhuV8B
$Plex = http://127.0.0.1:32400

[xml]$Library = (Invoke-WebRequest -Uri $Plex/library/sections$Token).Content

$LibraryItems = $Library.MediaContainer.Directory Write-Host What do you want to refresh?
$Counter = 0
foreach ($Element in $LibraryItems)
{
$Counter ++
$Title = $Element.Title
Write-Host $Counter $Title
}

$Selection = Read-Host
$Key = $library.MediaContainer.Directory[($Selection -1)].key

$DoRefresh = Invoke-WebRequest -Uri $Plex/library/sections/$key/refresh$Token
if ($DoRefresh.StatusCode -eq 200)
{
Write-Host OK, refreshing library
}
else
{
Write-Host Ooops, something went wrong:nn$DoRefresh
}

 

Alternatively you may just want to update a single library, in which case collect the ‘Key’ number for your library from the xml file mentioned above.  The below code just updates my Movies library.

$Token = ?X-Plex-Token=HbRpo18tDB364CaSkidhr
$Plex = http://127.0.0.1:32400
$Key = 8
$DoRefresh = Invoke-WebRequest -Uri $Plex/library/sections/$key/refresh$Token

 

For more information on this see the Plex article.

2 Replies to “Use PowerShell to Update your Plex Library with new content”

  1. seems to be a massive gap on how to actually run this script? like what tf am i to save this file as so that qbittorrent may update libraries after downloading?

    more explanation please.

    1. You save the script like any other powershell script. If you’ve never used powershell you might want to look up a tutorial first. To give you a head start open up Windows PowerShell ISE (This is software that comes preinstalled on Windows). Top part is to write/edit a script, bottom part is the console window for output or single line input. You can press F5 or click the green play/arrow button to run the script.

Leave a Reply

Your email address will not be published. Required fields are marked *