Install-PDFPrinter.ps1

function Install-PDFPrinter
  {
    <#
        .SYNOPSIS
        Installs a new Printer called "PrintPDFUnattended" which prints to file
 
        .DESCRIPTION
        Uses the built-in "PrintToPDF" printer driver to create a new printer
        that prints unattendedly to a fixed file in the temp folder
 
        .EXAMPLE
        Install-PDFPrinter
        Installs the printer "PrintPDFUnattended". Needs to be run only once.
        To remove the printer again, use this command:
        Remove-Printer PrintPDFUnattended
 
        .NOTES
        Requires the "PrintToPDF" printer driver shipping with Windows 10, Server 2016, or better
 
        .LINK
        URLs to related sites
        The first link is opened by Get-Help -Online Install-PDFPrinter
    #>



  
    $PrinterDefaultName = 'Microsoft Print to PDF'
    $printerName = 'PrintPDFUnattended'
    # choose a default path where the PDF is saved:
    $PDFFilePath = "$env:temp\PDFResultFile.pdf"
  

    # see whether the driver exists
    $ok = @(Get-PrinterDriver -Name $PrinterDefaultName -ea 0).Count -gt 0
    if (!$ok)
    {
      Write-Warning -Message "Printer driver 'Microsoft Print to PDF' not available."
      Write-Warning -Message 'This driver ships with Windows 10 or Server 2016.'
      Write-Warning -Message "If it is still not available, enable the 'Printing-PrintToPDFServices-Features'"
      Write-Warning -Message 'Example: Enable-WindowsOptionalFeature -Online -FeatureName Printing-PrintToPDFServices-Features'
      return
    }

    # check whether port exists
    $port = Get-PrinterPort -Name $PDFFilePath -ErrorAction SilentlyContinue
    if ($port -eq $null)
    {
      # create printer port
      Add-PrinterPort -Name $PDFFilePath 
    }

    # add printer
    Add-Printer -DriverName $PrinterDefaultName -Name $printerName -PortName $PDFFilePath 
  }