0

I am trying to make a Powershell script that will run in the OU so when the user shuts down their laptop it will remove the local printer and when they turn on their laptop it will re-add and rename the locally attached printer (this will allow the laptops to be docked into different rooms and still use the local printers). The startup script works fine. it re-adds the printer, renames it and sets the trays etc. however the shutdown script doesn't seem to be working correctly.

When the shutdown script is run it removes the printer from device manager/print manager/registry but it still seems to be showing in devices and printer as "driver not available". So when the laptop is started back up the startup script doesn't work as the printer is sitting in error state for the above. a work around is right clicking the printer in devices and printers and pressing "Remove Device" but obviously can not do this during shutdown.

Is there anyway to remove a device from "Devices and Printers" via Powershell? it does work with network printers but just won't fully remove local printers.

    $GetDriver = (Get-Printer -Name "Reports" | Select-Object DriverName | ConvertTo-Csv -NoTypeInformation -Delimiter ",") | % {$_ -replace '"',''} | Select-Object -Skip 1
$GetPort = (Get-Printer -Name "Reports" | Select-Object PortName | ConvertTo-Csv -NoTypeInformation -Delimiter ",") | % {$_ -replace '"',''} | Select-Object -Skip 1
  
Get-Printer -Name "Reports" | Rename-Printer -NewName $GetDriver
Remove-Printer -Name $GetDriver
Remove-PrinterPort -Name $GetPort

enter image description here

Thanks in advance for any help

  • Why are you using `ConvertTo-Csv` to get the properties for the printer? Wouldn't this be easier: `$printer = Get-Printer -Name "Reports"; Remove-Printer -Name $printer.Name; Remove-PrinterPort -Name $printer.PortName` – Theo Sep 13 '21 at 13:53
  • True! hadn't thought of that. Any idea on how to remove from devices and printers via powershell? – scotthannah Sep 14 '21 at 09:29

0 Answers0