Search code examples
powershellpowerpoint

How to extract media folder from a lot of .PPT files with PowerShell script?


I have a bunch of PowerPoint presentations, and I need to extract all the images out of every file. I've noticed (by opening one of those files with 7Zip) that it contains a 'Media' folder where all the images are located. is there any way to extract the 'Media' folder for every file, out to an individual folder for each file?


Solution

  • I wrote this really fast, so I wouldn't be surprised if there's a better way, or at least, a cleaner way to accomplish what you're after. It's written to work both in instances where there is, and isn't, an embedded media folder.

    For testing purposes, I had a folder on my desktop called pptx with four *.pptx files inside of it. Once the quick script completed, it had created a folder for each PowerPoint file in the same folder. In each of those folders is either a media folder with the files you're after, or a text file that indicates a media folder could not be found. Again, there's probably a cleaner way, but until then, this should work.

    $Path = 'C:\users\tommymaynard\Desktop\pptx'
    $Files = Get-ChildItem -Path $Path
    
    Foreach ($File in $Files) {
        New-Item -Path $File.DirectoryName -ItemType Directory -Name $File.BaseName | Out-Null
    
    If (Get-Command -Name Expand-Archive) {
        Expand-Archive -Path $File.FullName -OutputPath $File.FullName.Split('.')[0]
    
    } Else {
        Add-Type -AssemblyName System.IO.Compression.FileSystem
        [System.IO.Compression.ZipFile]::ExtractToDirectory($File.FullName,$File.FullName.Split('.')[0])
    } # End If-Else.
    
    If (Test-Path -Path "$($File.FullName.Split('.')[0])\ppt\media") {
        Move-Item -Path "$($File.FullName.Split('.')[0])\ppt\media" -Destination $File.FullName.Split('.')[0]
        Get-ChildItem -Path $File.FullName.Split('.')[0] | Where-Object {$_.Name -ne 'media'} | Remove-Item -Recurse
    
    } Else {
        Get-ChildItem -Path $File.FullName.Split('.')[0] | Remove-Item -Recurse
        New-Item -Path $File.FullName.Split('.')[0] -ItemType File -Name 'No media folder.txt' | Out-Null
    } # End If-Else.
    } # End Foreach.
    

    Edit: Using .NET is so much faster than Expand-Archive! If speed is what you're after, and you're running a version of PowerShell that includes Expand-Archive, then change Get-Command -Name Expand-Archive to $true -eq $false to force that .NET be used. Either that, or just dump the first If-Else and pull out that .NET code... let us know if you need further assistance.

    Edit2: I wrote about this post on my own blog: http://tommymaynard.com/extract-media-folder-from-powerpoint-files-2017/. It has an updated version of my code.