Help Converting Publisher Files To PDFs

K9 Camo Companions 0 Reputation points
2026-07-29T18:49:36.1966667+00:00

With publisher going away in October 2026, we desperately need a solution to easily convert THOUSANDS of publisher files into PDFs. I have looked through other forums and read that we could use the Microsoft script on PowerShell to convert the files. THIS DOES NOT WORK. It was stated that it was "easy" and you "don't have to be a PowerShell expert" those are incredibly incorrect statements. I have used every example script provided and receive this error:

Convert-PubFileToPDF.ps1 : The term 'Convert-PubFileToPDF.ps1' is not recognized as the name of a cmdlet, function,

script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is

correct and try again.

At line:1 char:1

  • Convert-PubFileToPDF.ps1 -Filter "*.pub" -Recurse
  • 
        + CategoryInfo          : ObjectNotFound: (Convert-PubFileToPDF.ps1:String) [], CommandNotFoundException
    
        + FullyQualifiedErrorId : CommandNotFoundException
    
    

I have no idea what I am doing wrong. I have looked into other PDF converters and they all have limits from 3MB - 1GB, which will take weeks to convert all the files. At this point, I am begging you to provide an actual simple solution, or please keep Publisher available for those that already have it installed.

Microsoft 365 and Office | Publisher | For business
0 comments No comments

3 answers

Sort by: Most helpful
  1. Shawn Sloan 0 Reputation points
    2026-08-05T17:30:02.14+00:00

    We ran into issues/error messages caused by long file names and wildcard characters. This is the adjusted script to handle those issues; I converted 25 years worth/ 33,000 files with this script. So far the PDF files are perfect.

    <#
    .SYNOPSIS
    	Converts Microsoft Publisher .pub files to PDF format.
    .DESCRIPTION
    	This script automates the conversion of Microsoft Publisher (.pub) files to PDF format using the Microsoft Office Interop Publisher library.
    	It processes all files matching the specified filter, checks if a PDF already exists for each file, and skips conversion if the PDF is present.
    	The script logs successful conversions and any errors encountered during the process.
        Updated to fully support Windows Long Paths (>260 characters) and wildcard filters.
    .PARAMETER Filter
    	Specifies the file filter to select Publisher files for conversion.
    	This can be a specific file name (e.g., "document.pub") or a wildcard pattern (e.g., "*.pub").
    .PARAMETER Recurse
    	If specified, searches for Publisher files recursively in all subdirectories that match the filter. If omitted, only the current directory is searched.
    #>
    param
    (
    	[ValidateNotNullOrEmpty()]
    	[string]
    	$Filter,
    	[switch]
    	$Recurse
    )
    if (-not $PSBoundParameters.ContainsKey('Filter')) {
    	Write-Error "The -Filter parameter is required."
    	exit 1
    }
    if (-not ($Filter -like "*.pub")) {
    	Write-Error "The filter must specify .pub files (e.g., '*.pub' or 'file.pub').";
    	exit 1;
    }
    try {
    	# Safely determine the search directory and file pattern without letting GetFullPath choke on wildcards
    	$resolvedFilter = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Filter)
    	
    	if ([System.IO.Directory]::Exists($resolvedFilter)) {
    		$searchDir = $resolvedFilter
    		$searchPattern = "*.pub"
    	} else {
    		# Split the path into directory and file pattern parts safely
    		$searchDir = [System.IO.Path]::GetDirectoryName($resolvedFilter)
    		$searchPattern = [System.IO.Path]::GetFileName($resolvedFilter)
    		
    		# If the directory part still contains wildcards, strip them to find the base path
    		if ($searchDir -like "*[*?]*") {
    			Write-Error "Wildcards are only supported in the filename/extension part of the filter, not in folder names."
    			exit 1
    		}
    	}
    	if (-not [System.IO.Directory]::Exists($searchDir)) {
    		Write-Error "The directory does not exist: $searchDir"
    		exit 1
    	}
    	# Use modern dotnet enumeration which natively ignores the 260 character path limit
    	$searchOption = if ($Recurse) { [System.IO.SearchOption]::AllDirectories } else { [System.IO.SearchOption]::TopDirectoryOnly }
    	try {
    		$rawFiles = [System.IO.Directory]::EnumerateFiles($searchDir, $searchPattern, $searchOption)
    		$files = @()
    		foreach ($f in $rawFiles) {
    			$files += New-Object System.IO.FileInfo($f)
    		}
    	} catch {
    		Write-Error "Error finding files: $_"
    		exit 1
    	}
    	if ($files.Count -eq 0) {
    		Write-Error "No Publisher files found for the filter: $Filter";
    		exit 1;
    	}
    	Write-Output "Running...";
    	Add-type -AssemblyName Office;
    	Add-type -AssemblyName Microsoft.Office.Interop.Publisher;
    	try {
    		$app = New-Object -ComObject Publisher.Application;
    	} catch {
    		Write-Error "Microsoft Publisher is not installed or accessible.";
    		exit 1;
    	}
    	$successCount = 0;
    	$failCount = 0;
    	foreach ($file in $files) {
    		if ($file.Extension -eq ".pub") {
    			$fileFullName = $file.FullName;
    			
    			# Prepend long path escape prefix if the path length exceeds standard limits
    			if ($fileFullName.Length -ge 240 -and -not $fileFullName.StartsWith("\\?\")) {
    				if ($fileFullName.StartsWith("\\")) {
    					# Network UNC paths
    					$fileFullName = "\\?\UNC\" + $fileFullName.Substring(2)
    				} else {
    					# Local drive paths
    					$fileFullName = "\\?\" + $fileFullName
    				}
    			}
    			$pdfFilePath = [System.IO.Path]::ChangeExtension($fileFullName, '.pdf')
    			
    			# Use dotnet's Exists check to circumvent standard Test-Path path length limitations
    			if ([System.IO.File]::Exists($pdfFilePath)) {
    				Write-Error "PDF file already exists: $pdfFilePath";
    				$failCount++;
    				Continue;
    			}
    			# Open the file
    			try {
    				$doc = $app.Open($fileFullName);
    			} catch {
    				$failCount++;
    				Write-Error "Error opening file: $fileFullName $_";
    				Continue;
    			}
    			if (-not($doc)) {
    				$failCount++;
    				Write-Error "Failed to open file: $fileFullName";
    				Continue;
    			}
    			try {
    				# Export file as PDF
    				$doc.ExportAsFixedFormat([Microsoft.Office.Interop.Publisher.PbFixedFormatType]::pbFixedFormatTypePDF, $pdfFilePath);
    				if ([System.IO.File]::Exists($pdfFilePath)) {
    					Write-Output "Exported to $pdfFilePath.";
    					$successCount++;
    				} else {
    					$failCount++;
    					Write-Error "Failed to export file: $fileFullName";
    				}
    			} catch {
    				$failCount++;
    				Write-Error "Error during export: $_";
    			}
    			$doc.Close();
    		}
    	}
    	#Log output
    	Write-Output "Converted $successCount files with $failCount errors.";
    }catch{
    	Write-Error $_;
    }finally {
    	if ($app) {
    		#Quit Publisher
    		$app.Quit();
    	}
    }
    

    Was this answer helpful?

    0 comments No comments

  2. Hendrix-C 19,970 Reputation points Microsoft External Staff Moderator
    2026-07-29T20:37:51.8233333+00:00

    Hi,

    The PowerShell script by Microsoft does provide a free way to batch convert all .pub files to PDF. However, the process does not just simply paste Convert-PubFileToPDF.ps1 -Filter "*.pub" -Recurse and then it runs. I will provide you the guidance in the most detailed and easiest way to follow:

    User's image

    • After that, go to C:\ and create a new folder named PubConvert > copy the downloaded file (named Convert-PubFileToPDF.ps1) into this folder
    • Then put all your .pub files into a single folder, for example: PublisherFiles in C:\ like this

    User's image

    After that, open Windows PowerShell and follow this instruction:

    • Paste cd C:\PubConvert > press Enter
    • Paste Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass > Enter > if you see a prompt asking for confirmation, press Y and Enter
    • Paste Start-Transcript -Path "C:\PubConvert\PublisherConversionLog.txt" > Enter
    • Paste .\Convert-PubFileToPDF.ps1 -Filter "C:\PublisherFiles\*.pub" -Recurse > Enter
    • Wait until it finished and returns a message "Converted xxx files with xxx errors" > paste Stop-Transcript > Enter and close PowerShell.
    • This is a sample PowerShell interface if everything is done properly

    User's image

    • Then go to the PublisherFiles folder and you will see all your .pub files are converted to PDFs.

    You can give it a try and let me know whether it works. If you have any other questions, please feel free to reach out again in the comments on this post. I'm happy to assist you further.

    Thank you for your understanding and cooperation. I'm looking forward to your reply.


    If the answer is helpful, please click "Yes" and kindly upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


  3. Marcin Policht 102.2K Reputation points MVP Volunteer Moderator
    2026-07-29T20:21:35.1533333+00:00

    The error is not coming from the conversion script itself. PowerShell is telling you it cannot find the script to run. This usually means you are not in the folder containing Convert-PubFileToPDF.ps1 or you did not specify its path.

    First, check your current folder:

    Get-Location
    

    Then check whether the script is actually there:

    Get-ChildItem Convert-PubFileToPDF.ps1
    

    If the script is in the current folder, run it like this:

    .\Convert-PubFileToPDF.ps1 -Filter "*.pub" -Recurse
    

    If the script is in another folder, specify the full path, for example:

    C:\Scripts\Convert-PubFileToPDF.ps1 -Filter "*.pub" -Recurse
    

    This script also requires the desktop version of Microsoft Publisher to be installed, since it automates Publisher through COM. If Publisher is not installed, the script will fail when it tries to create the Publisher.Application object.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.