Install Printers from XML Files

0

I backed up printers from a Windows 10 system to XML files. I'm trying to add them using the Set-Printconfiguration CMDLET, but it seems to be not accepting variables?

I've looked everywhere but I cannot find anything saying my syntax is wrong.

#get list of printers in backup folder
$printerNames = (Get-ChildItem -Path c:\temp\printers\*.xml -Recurse | select name).name
    foreach ($printer in $printerNames)
        {
        Set-PrintConfiguration -PrinterName $printer -PrintTicketXml c:\temp\printers\$printer
        }

Here is the code I used to get the printer XML files:

$TARGETDIR = "c:\temp\printers"
if(!(Test-Path -Path $TARGETDIR )){
    New-Item -ItemType directory -Path $TARGETDIR
}

# Get all the printers:
    $PN = (get-printer | select name).name

# Foreach loop to create XML file for each printer configuration
    Foreach ($P in $PN){
    $GPC = get-printconfiguration -PrinterName $P
    mkdir c:\temp\printers\$P 
    $GPC.PrintTicketXML | out-file C:\Temp\printers\$P.xml
# $p|select *|out-file -Path c:\temp\$p.xml -NoTypeInformation
    }

edit: here is the error I'm getting:

Set-PrintConfiguration : The specified printer was not found.
At U:\PowerShell\Scripts\backup\newRestorePrinters.ps1:15 char:9
+         Set-PrintConfiguration -PrinterName $printer -PrintTicketXml  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_PrinterConfiguration:ROOT/StandardCi...erConfiguration) [Set-PrintConfiguration], CimException
    + FullyQualifiedErrorId : HRESULT 0x80070709,Set-PrintConfiguration

edit I added more variables to get the list of printers w/out the .XML on the end of the names. It still gives me the same error output. It looks like it's not passing my variables to the set-printconfiguration command?

New restore script code:

$printerShortNameList = (Get-ChildItem -Path c:\temp\printers\*.xml -Recurse | select name).name
    foreach ($shortName in $printerShortNameList)
        {
        $shortName.Replace('.xml', "x")
        }

#get list of printers in backup folder
$printerNames = (Get-ChildItem -Path c:\temp\printers\*.xml -Recurse | select name).name
    foreach ($printer in $printerNames)
        {
        Set-PrintConfiguration -PrinterName $shortName -PrintTicketXml c:\temp\printers\$printer
        }
powershell
asked on Stack Overflow May 6, 2019 by superKing • edited May 8, 2019 by superKing

2 Answers

1

What was the error message? Shouldn't you take the '.xml' off the end of $printer for the printer name? I think you have to use add-printer first. I don't believe Set-PrintConfiguration creates printers.

On the bottom when you make the xml files, why do you create the c:\temp\printers\$p directory?

answered on Stack Overflow May 7, 2019 by js2010
0

Set-PrintConfiguration:

You need administrator credentials to use Set-PrintConfiguration.

answered on Stack Overflow May 7, 2019 by Moerwald

User contributions licensed under CC BY-SA 3.0