Public/Set-SpecPrinterSettings.ps1

Function Set-SpecPrinterSettings {
    <#
    .SYNOPSIS
        Restores printer settings for specific printers from saved configuration files.
 
    .DESCRIPTION
        This function restores printer settings for specific printers by applying configuration files that were previously saved in the "C:\temp" directory.
        The function loops through the list of printers and constructs the corresponding file name based on the printer's model and persona.
        If a configuration file exists for a printer, and the $DefaultPrinterUpdate parameter is set to $true, the function applies the settings to the printer using PRINTUI.DLL.
 
    .PARAMETER DefaultPrinterUpdate
        Specifies whether the default printer settings should be updated. If set to $true, the function applies the saved settings to the default printer(s).
        This parameter is mandatory.
 
    .EXAMPLE
        Set-SpecPrinterSettings -DefaultPrinterUpdate $true
        # Restores printer settings for the default printer(s) from saved configuration files.
 
    .OUTPUTS
        None
        This function does not generate any output; it applies printer settings silently using PRINTUI.DLL.
 
    .NOTES
        Author: owen.heaume
        Version: - 1.0
                 - 1.1 Wrap PRINTUI.dll into start-process so pester tests can be used.
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory = $true)]
        [bool]$DefaultPrinterUpdate
    )

    ForEach ($printer in (get-printer).name) {
        $PrinterModelSplit = ($printer.Split("_")[0, 1]) -join ("_")
        $PrinterPersonaSplit = ($printer.Split("_")[2])

        If ($PrinterPersonaSplit -eq "TR") {
            if ($printer.Split("_")[3]) {
                $PrinterPersonaSplit = ($printer.Split("_")[2]) + "_" + ($printer.Split("_")[3])
            }
        }

        $PrinterSettingsFile = $PrinterModelSplit + "_" + $PrinterPersonaSplit

        # restore printer settings by PRINTUI.DLL
        If ((Test-Path "C:\temp\$PrinterSettingsFile.dat") -and $DefaultPrinterUpdate -eq $true) {
            Write-Verbose "File found $PrinterSettingsFile.dat for printer $printer"
            $rundll32Args = "PRINTUI.DLL,PrintUIEntry", "/Sr", "/n", $printer, "/a", "`"C:\temp\$printerSettingsFile.dat`"", "/q", "g", "d", "r"
            Start-Process -FilePath "C:\Windows\System32\rundll32.exe" -ArgumentList $rundll32Args -Wait
        } else {
            Write-Verbose "No settings change for printer $printer"
        }
    }
}