I was able to download all the legacy image files in my media library via an ASP.net script (https://gist.github.com/DaveGoosem/6289015), in order to optimize them. I've finished doing this and now want to re-upload/attach them to the media items and replace the old ones. Wondering if there is an easy way to do this either via .Net or via a Powershell script? There are hundreds, so I'm not keen on replacing them manually within the content editor!

  • You are probably better off using SPE Remoting to download, run optimizer, and upload rather than mix it up with the page. Commented Apr 17, 2017 at 20:49
  • What tool you used to optimize images? Commented Dec 18, 2017 at 10:54

2 Answers 2


This can certainly be performed using Sitecore PowerShell Extensions. Read more about setting up SPE Remoting here.

Example: The following gets an item and uploads through SPE Remoting.

$session = New-ScriptSession -Username "sitecore\admin" -Password "b" -ConnectionUri "https://remotesitecore"
$localFilePath = "C:\temp"
$filename = "kitten.jpg"
$filenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($filename)
Get-Item -Path "$($localFilePath)\$($filename)" | Send-RemoteItem -Session $session -RootPath Media -Destination "Images/"

If you wish to update using a GUID then the command changes slightly like this:

... | Send-RemoteItem -Session $session -RootPath Media -Destination "{GUID}"

Check out the modules directory for more samples here.

  • So, the problem is that our media library has many folders so I can't just dump them all into a single directory. You think it would be difficult to look for the originating item by the filename and then re-attach it to the appropriate item in the correct folder? Commented Apr 17, 2017 at 20:36
  • Not at all. I've updated the example to show how to update with the item ID. Commented Apr 17, 2017 at 20:39
  • Interesting. So that will grab the filename from the local folder, match that to an item based on the attachment, and then upload that file and replace the attachment with the new version? Or are you assuming that the local store's file name has the GUID in it? They do not, they are regular descriptive filenames... Commented Apr 18, 2017 at 12:47
  • You can either use the filename or GUID to replace the blob for an existing item. If for example you downloaded the item and maintained a csv of IDs then the GUID would help. Perhaps sticking with the filename and match on that is your best option here. Commented Apr 18, 2017 at 12:49
  • Ok, well, I've found I think another way to do this - using the Media Conversion Tool module to convert items to using the file system, then optimizing the items in place. After that we may or may not reconvert them back to db blobs. This will require doing the optimization process again, but that seems a much easier lift than working through the powershell scripting syntax which I'm pretty unfamiliar with in order to work properly... Commented Apr 18, 2017 at 16:20

I would recommend implementing optimisation inside the script. So for each image, you get media stream and optimise it in memory and then reattach to the same item. For this, you can use Dianoga (lossless optimisation) or any other tool like ImageMagick, jpegtran, jpegoptim.

For example for Dianoga additional code can looks like this:

foreach (var image in images)
    var mediaItem = (MediaItem)image;
    var media = MediaManager.GetMedia(mediaItem);
    var stream = media.GetStream();

    var dianogaOptimizer = new Dianoga.MediaOptimizer();
    var optimizedOutputStream = dianogaOptimizer.Process(stream, new MediaOptions { });
    if (optimizedOutputStream?.Stream != null)
        // REATTACH
        media.SetStream(optimizedOutputStream.Stream, media.Extension);

For the future, you can consider optimising images during upload to Sitecore as I described it here: How to resize and compress images with Dianoga during upload to sitecore media library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.