Requirements

The code was tested with Office 2016


Solution


Word

The code creates a COM Object for Word, then goes on to make the application invisible so that it doesn't show up every time the script gets executed. The document then gets opened in the background in Word, in this example the document is in "C:\Temp\Document.docx". The extension *.DOCX gets replaced by *.PDF and the file is saved in the same location. You will not lose the original document as the function that is called is "SaveAs". The code then proceeds to close the open document and quit the application.

#Creates a Word-Object
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.Documents.Open("C:\Temp\Document.docx")
#Creates the PDF file
$name = ($doc.FullName).replace('docx', 'pdf')
$doc.SaveAs($name, 17)
$doc.Close()
$word.Quit()


Excel

Converting an Excel file to a PDF works the same way it works in Word.

#Creates a Excel-Object
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$formatPDF = 17
$saveaspath = "C:\TEMP\WorkBook.pdf"
$workbook = $excel.Workbooks.Open("C:\TEMP\WorkBook.xlsx")
#Creates the pdf-file
$workbook.SaveAs($saveaspath ,$formatPDF)
$workbook.Close()
$excel.Quit()


Powerpoint

In powerPoint the code looks different, some special parameters have to be passed when opening the presentation. "msoFalse" is the equivalent of $false in powerShell. Also in the "SaveAs" method some special parameters are needed to convert the presentation to a PDF file

$powerpnt = New-Object -ComObject PowerPoint.Application
$doc = "C:\Temp\Presentation.pptx"
$saveaspath = "C:\Temp\Presentation.pdf"
$openDoc = $powerpnt.Presentations.Open($doc,[Microsoft.Office.Core.MsoTriState]::msoFalse,[Microsoft.Office.Core.MsoTriState]::msoFalse,[Microsoft.Office.Core.MsoTriState]::msoFalse)
$openDoc.SaveAs($saveaspath , [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF,[Microsoft.Office.Core.MsoTriState]::msoFalse)
$openDoc.Close()
$powerpnt.Quit()


See Also

PowerPoint Presentation.Open Method (Microsoft Docs)

PowerPoint Presentation.SaveAs Method (Microsoft Docs)