A PowerShell script for bulk converting Microsoft Office documents and other file types to PDF format.
This script provides a robust solution for converting various file types to PDF format using Microsoft Office automation. It handles Word documents, Excel spreadsheets, PowerPoint presentations, images, and other text-based files.
-
Multi-format support: Converts a wide range of file formats including:
- Word documents (.doc, .docx)
- Excel spreadsheets (.xls, .xlsx, .csv)
- PowerPoint presentations (.ppt, .pptx)
- Text files (.txt, .rtf)
- Web pages (.htm, .html)
- Images (.jpg, .jpeg, .png, .gif, .tif, .tiff, .bmp)
-
Batch processing: Convert entire folders of documents at once
-
Subfolder support: Option to recursively process files in subfolders
-
Custom output location: Specify a separate folder for the converted PDF files
-
Selective conversion: Option to delete original files after successful conversion
-
Skip existing: Automatically skips files that have already been converted to avoid duplication
-
Memory management: Restarts Office COM objects periodically to prevent crashes during large batch jobs
-
Detailed logging: Color-coded console output shows conversion progress and results
- Windows operating system
- PowerShell 3.0 or higher
- Microsoft Office installed (Word, Excel, and PowerPoint)
- Save the script file as
Convert-OfficeToPDF.ps1
to your desired location - Open PowerShell with administrator privileges
- Navigate to the folder containing the script
- You may need to set the execution policy to run the script:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Convert all supported files in a folder:
.\Convert-OfficeToPDF.ps1 -FolderPath "C:\Documents"
Convert files with various options:
.\Convert-OfficeToPDF.ps1 -FolderPath "C:\Documents" -OutputPath "C:\PDFs" -IncludeSubfolders -DeleteOriginal
You can also import the function into your PowerShell session:
. .\Convert-OfficeToPDF.ps1
Convert-OfficeToPDF -FolderPath "C:\Documents" -OutputPath "C:\PDFs"
Parameter | Type | Required | Description |
---|---|---|---|
FolderPath | String | Yes | Path to the folder containing files to convert |
OutputPath | String | No | Path to save the PDF files (defaults to input folder) |
DeleteOriginal | Switch | No | If specified, original files will be deleted after successful conversion |
IncludeSubfolders | Switch | No | If specified, files in subfolders will also be processed |
FileTypes | String[] | No | Array of file extensions to process (defaults to all supported types) |
Convert all supported files in a folder to PDF, saving them in the same location:
Convert-OfficeToPDF -FolderPath "E:\Documents"
Convert all supported files, including those in subfolders, and save PDFs to a different location:
Convert-OfficeToPDF -FolderPath "E:\Documents" -OutputPath "E:\PDFs" -IncludeSubfolders
Convert only Word documents and delete the originals after successful conversion:
Convert-OfficeToPDF -FolderPath "E:\Documents" -FileTypes @('.doc','.docx') -DeleteOriginal
- Files not converting: Ensure Microsoft Office is installed and functioning correctly
- 'Access denied' errors: Run PowerShell as Administrator
- COM errors: Make sure no Office applications are running when executing the script
- Memory issues: Try converting smaller batches of files at a time
- Requires Microsoft Office to be installed
- May not preserve all formatting perfectly in complex documents
- Performance depends on your system resources and the complexity of documents
This script is released under the MIT License.
This script was created to simplify the process of converting large collections of documents to PDF format automatically.