Extracting media files from a PowerPoint presentation with PowerShell

Ever needed to extract all the media files (images, video etc.) from a PowerPoint presentation? Copy/paste does it, but getting the stuff into separate files is a chore. Unless you speak fluent PowerShell, that is.

The following script defines a function that takes a full path (including the directory) to the PowerPoint file. It then proceeds to extract the media files from the pptx package to a separate directory. If you have “D:\Temp\BusinessProposal.pptx”, you get “D:\Temp\BusinessProposal.pptx_media\image1.jpeg” and so on.

A typical usage might be Export-MediaFromPptx D:\temp\BusinessProposal.pptx, or if you need to run it on several files, you might do dir *.pptx | foreach { Export-MediaFromPptx $_.FullName }.

Download the script here

   1:  function Export-MediaFromPptx($pptxFile)
   2:  {
   3:      [void] [System.Reflection.Assembly]::LoadFrom(
               "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll")
   4:      $pkg = [System.IO.Packaging.Package]::Open($pptxFile)
   6:      $mediaDirectory = [System.IO.Path]::GetDirectoryName($pptxFile) + "\" +
                [System.IO.Path]::GetFileName($pptxFile) + "_media"
   7:      md $mediaDirectory | out-null
   9:      foreach ($mediaPart in
                ($pkg.GetParts() | where { $_.Uri.ToString().StartsWith("/ppt/media/") })) {
  11:        $sourceStream = $mediaPart.GetStream()
  12:        [byte[]]$buffer = new-object byte[] $sourceStream.Length
  13:        [void]$sourceStream.Read($buffer, 0, $sourceStream.Length)
  14:        $sourceStream.Close()
  16:        $targetFileName = $mediaDirectory + "\ " +
                 $mediaPart.Uri.ToString().Remove(0, "/ppt/media/".Length)
  17:        [System.IO.File]::WriteAllBytes($targetFileName, $buffer)
  18:      }
  20:      $pkg.Close()
  21:  }

In case you need to modify the script, here’s a rough guide on how it works:

  • Line 3 loads up WindowsBase.dll to allow usage of System.IO.Packaging, the .NET 3.0 APIs for managing Open Packaging Conventions documents (XPS and Office 2007+ files)
  • Lines 6-7 define the target directory for file export; if you want to gather all the media from multiple presentations into a single directory, just use a fixed path here (but see below for line 16).
  • Line 9 uses the Packaging API to query for package parts that reside in the directory /ppt/media. If you know OPC, you know it would be more correct to use part relationships to navigate to media, but this works in practice and is very simple to use.
  • Lines 11-14 read the media from the stream, and line 17 writes it into the target file.
  • Line 16 defines the target file name. If you coalesce files from several presentations to one directory, you’ll want to do some prefixing here. By default, the file names created by PowerPoint are used, meaning that files will be named in sequence (“image16.png” and so on).

Hope it’s useful for somebody :-)

April 5, 2010 · Jouni Heikniemi · 8 Comments
Tags: ,  · Posted in: .NET

8 Responses

  1. Timo L. - April 5, 2010

    Nice script, but why not just extract the zipped presentation file with your favorite compression utility and access your media files inside the /ppt/media folder? :)

    Works with .pptx files and also with Apple Keynote .key files.

    Of course the script above comes handy if you need to extract media files from several documents. I bet there is even an AppleScript to do that if you're on a Mac (if there isn't, it wouldn't be too difficult to create one).

    But it is really nice to see that Windows finally has a powerful scripting tool like Unix-based systems have had for decades.

  2. Jouni Heikniemi - April 5, 2010

    You could quite well just unzip the presentation as you said. As usual, PowerShell is only necessary when automating a process (running automatically at a given trigger) or processing a large, repeatable number of runs for the same algorithm. This applies here as well.

    Also, the PS script could function by unzipping the archive instead of using the .NET Packaging APIs. But since zipping includes taking a dependency either on an external library or a COM object (Windows Shell), I think the pure .NET BCL approach is better.

  3. Ricky Rameau - October 11, 2018

    Wow, that’s what I was looking for, what a material!existing here at this blog, thanks admin of this web page.

  4. Smithb4 - February 2, 2019

    Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way Ill be subscribing to your feeds and even I achievement you access consistently rapidly. ecfecedbedecckde

  5. buy review movie - March 23, 2019

    buy review movie

    Edit a paper online editing services Buy resume.. Buy resume help., Edit resume..

  6. Acronis Cracks - May 1, 2019

    Acronis Cracks

    buy cheap adobe creative suite buy cheap adobe creative suite. buy cheap adobe creative suite, buy cheap adobe creative suite.

  7. Abe Rosebush - August 15, 2020

    If you want you can watch a huge black bull penetrating my wifes ass!

  8. Augustine Zamacona - January 12, 2021

    An array of wild hair caution equipment in hair apply, tweezers, hydrogen stick reviews frizzy hair scissors, frizzy hair sawing scissors, sheers, specialist sheers, frizzy hair sheers, frizzy hair hair comb, bobby pin, head piece, eyelash curler, hair hair brush, plus shower limitation accessories can be purchased.

Leave a Reply