← Field Notes

Automating Document Summarization with Power Automate Desktop and PowerShell

What if AI summarized your entire hard drive?

If you're anything like me, you have folders upon folders inside of more folders, stuffed with documents ranging from interesting ideas to early drafts to completed projects, and you're not sure what to do about them other than throw it all into an Archive folder and forget about it.

Or maybe you're assiduously collected and organized a carefully curated library of documents containing your life's work and the work of your esteemed colleagues and you're worried that something might happen to it. Oh, I don't know, ransomware? Or imagine that in 48 hours an incoming meteor will destroy all your storage and backups, and you'll be lucky to get out with a thumb drive. What's on it?

That's why I thought it might be useful to experiment with putting ChatGPT to work in a methodical fashion, looking at one file at a time throughout cloud and local hard drives.

I wrote a Power Automate Desktop (PAD) flow that does the following:

  1. Finds matching files. Identifies all Word (.doc, .docx) files inside a selected folder and its subfolders, including all nested levels.

  2. Loops through each. One at a time, PAD opens each Word document in read-only mode just long enough to copy the full text into memory and then closes the file.

  3. Sends each to ChatGPT. Provide a custom prompt followed by the text. Mine is a simple summarization request, but you might also try: "Please review this file for this list of problematic phrases and rewrite to ensure that a keyword search does not identify this document."

  4. Displays results. Show the chat response in a dialog box. One drawback of PAD is that you cannot perform the "Create text with GPT (preview)" action without following it immediately with a "Display message" action. Every item of generated text must be manually approved, which I'm sure prevents all sorts of bad behavior, but it's an obstacle to automation.

  5. Saves the results to a table. Puts the results into a simple Dataverse table created for this purpose, including fields for the original file path, the full-text content from Word, and the generated content. Other possibilities include saving as text or CSV, adding a row to an Excel table, or (if you live dangerously) write the results back into the original Word doc.

A good start

This was my first time building something in Power Automate Desktop, and I'm only beginning to wrap my head around it. My initial flow works fine for a relatively small group of files, but the main problem is having to sit around to press OK for each generated answer.

That's why I've come up with a few ideas for improvements.

Make it robust. Put in better error-handling and recovery. I gather this is something you learn from experience. Or from the comment section.

Move the GPT step to the cloud. To avoid the stop-and-go of manual review, I have a version of this flow that skips the GPT step in PAD. Now, with the full text of the Word documents stored in a Dataverse table, I can easily run a Power Automate Cloud flow that performs a GPT action on each document that stores the results back into the database without human confirmation. (Read about Power Automate prompts, and why human review is usually part of the workflow, in Microsoft Learn: Use your prompt in Power Automate.)

Expand the range of files that can be processed. No reason to stop at Word files. I haven't tried it yet, but PAD works with web pages, PDFs, Excel, SharePoint, OneDrive, OneNote, emails, and databases. There's even computer vision to analyze, describe, and tag images. If you're authorized to see it, PAD can open it.

Split big rocks into little rocks. I have some journal files that go on and on. (Can you believe it?) That's why I wrote a separate set of PowerShell scripts that split lengthy Word files into smaller files using Markdown format. That way, I can analyze the smaller files on their own.

Generate metadata. The summarization step is just the first pass. I have other ideas for how to perform automated analysis, detect and remove duplicates, and generate keywords to create linkages and surface insights hidden within the archive.

Keep an eye on the meter. I started this project on the last day of January trying to figure out how to use an entire month's worth of unused AI Builder credits. I wasn't about to let them expire without an experiment.

Every month I get 10,000 AI Builder Credits -- 5,000 each from Power Automate Premium and Power Apps Premium, which costs $40 per month plus tax. According to my calculations, without additional charge I can process 350 documents using GPT-4o, or 7,142 documents using GPT-4o Mini. (Assumptions: Document length of 700 words, response of 250 words.) That would cost me at least a couple bucks using OpenAI, plus the time it takes to figure it all out.

I imagine there are many ways to do this with more sophistication and at scale.

But if you're already using Premium editions of Power Platform, use your allotment!

And watch out for meteors.

Tech note: How to call PowerShell from PAD

PAD has an action called "Run PowerShell script" that executes whatever you throw in there, storing the output (or script error) into a variable.

I suppose what you're meant to do is copy-and-paste your PowerShell code directly into the window. But that's not how I did it.

Instead, I used VS Code with Github Copilot to write and test my script. That way is easier and faster, and I can maintain my code in a git repository.

No-Parameter PowerShell

Here's the script (listWordFiles.ps1).

# Uncomment the following line to set the directory path manually for testing

# $directoryPath = "C:\Path\To\Your\Directory"

# Check if the directory path is provided

if (-not $directoryPath) {

exit

}

# Get all Word files recursively

$wordFiles = Get-ChildItem -Path $directoryPath -Recurse -Include *.doc, *.docx

# Output the list of Word file paths to the host, wrapped in double quotes and delimited by carriage returns

$wordFilesPaths = $wordFiles | ForEach-Object {

'"' + $_.FullName + '"'

}

$csvOutput = $wordFilesPaths -join "`r`n"

$csvOutput

The usual practice with PowerShell is to use parameters. But there's no simple way to send a parameter using the "Run PowerShell script" action in PAD. Although you can embed a variable if you paste the entire script in -- which I didn't want to do.

My workaround was to concatenate a variable assignment with the script variable.

Flow walkthrough

Development notes. Screenshots below.

← Field Notes