Skip to content

Instantly share code, notes, and snippets.

@Santaro255
Created April 26, 2019 08:05
Show Gist options
  • Save Santaro255/087b984c956d186ded5135d10cffd760 to your computer and use it in GitHub Desktop.
Save Santaro255/087b984c956d186ded5135d10cffd760 to your computer and use it in GitHub Desktop.
##### edit #############
$computer = "localhost"
$printerName = "HP Color LaserJet CP2020 Series PCL 6"
$portName = "\\HOSTNAME\HP Color LaserJet CP2020 Series PCL6"
$driverPath = "\\HOSTNAME\Drivers\hp2025"
$inf_x86 = "hppcp610.inf"
$inf_x64 = "hppdp610.inf"
#########################
#platform
switch ($ver = (Get-WmiObject -Class win32_operatingsystem).OSArchitecture) {
{ $ver -like "32*" } { $driverInf = "$driverPath\$inf_x86"
$platform = "Windows NT x86" }
{ $ver -like "64*" } { $driverInf = "$driverPath\$inf_x64"
$platform = "Windows x64" }
}
#
#create local port
switch ($req = (Get-Service -ComputerName $computer -Name RemoteRegistry).Status) {
{ $req -eq "Stopped" } { sc.exe \\$computer start RemoteRegistry | Out-Null }
{ $req -eq "Running" } { continue }
}
$registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", "$computer")
$registryKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"
$regKey = $registry.OpenSubKey($registryKey, $true)
$regKey.SetValue("$portName", "")
sc.exe \\$computer stop spooler | Out-Null
sc.exe \\$computer start spooler | Out-Null
#
#install printer driver
$wmidriver = [wmiclass]"\\$computer\root\cimv2:Win32_PrinterDriver"
$wmiDriver.psbase.scope.options.enablePrivileges = $true
$driver = $wmiDriver.CreateInstance()
$driver.Name = $printerName
$driver.SupportedPlatform = $platform
$driver.Version = "3"
$driver.DriverPath = $driverPath
$driver.Infname = $driverInf
$wmiDriver.AddPrinterDriver($driver)
$wmiDriver.Put()
#
#install printer
$wmiPrinter = [wmiclass]"\\$computer\root\cimv2:Win32_Printer"
$printer = $wmiPrinter.CreateInstance()
$printer.Name = $printerName
$printer.Caption = $printerName
$printer.DriverName = $printerName
$printer.PortName = $portName
$printer.DeviceID = $printerName
$printer.Put()
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment