当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

作者:精品下载站 日期:2024-12-14 20:00:51 浏览:14 分类:玩电脑

在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序


在上一篇文章中,我们研究了可用于从 Windows XP 开始的所有 Windows 版本中管理打印机和打印操作的老式 VBS 脚本。今天,我们将考虑使用 PowerShell 安装、管理和删除打印机、打印端口、驱动程序和队列的典型命令。这些通过 PowerShell CLI 管理打印机的方法可用于现代操作系统 - Windows 10/8.1 和 Windows Server 2019/2016/2012 R2。

PowerShell 模块:PrintManagement

随着 Windows 8.1 和 Windows Server 2012 R2 的发布,Microsoft 发布了新版本的 PowerShell 4.0(Windows Management Framework 4.0 的一部分),显着扩展了基于 Windows 的打印服务器管理 cmdlet 的列表。您可以使用以下命令获取 Windows 10 (PowerShell v5) 上 PrintManagement 模块中可用的打印、驱动程序和打印队列管理 cmdlet 的完整列表:

Get-Command -Module PrintManagement

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

  • Add-Printer - 添加(安装)新打印机;

  • Add-PrinterDriver - 安装新的打印驱动程序;

  • Add-PrinterPort - 创建本地打印端口;

  • Get-PrintConfiguration - 显示打印机配置;

  • Get-Printer - 显示计算机上安装的打印机列表;

  • Get-PrinterDriver - 显示已安装的驱动程序列表;

  • Get-PrinterPort - 显示打印机端口列表;

  • Get-PrinterProperty - 显示打印机属性;

  • Get-PrintJob - 获取打印机打印作业列表;

  • Read-PrinterNfcTag - 从NFC标签获取打印机信息;

  • Remove-Printer - 删除打印机;

  • Remove-PrinterDriver — 删除打印机驱动程序;

  • Remove-PrinterPort - 删除打印机端口;

  • Remove-PrintJob - 删除打印机上的打印作业;

  • Rename-Printer - 重命名打印机;

  • Restart-PrintJob - 重新启动打印作业;

  • Resume-PrintJob - 恢复暂停的打印作业;

  • Set-PrintConfiguration - 设置打印机配置;

  • Set-Printer - 更新打印机配置;

  • Set-PrinterProperty - 更改打印机属性;

  • Suspend-PrintJob - 暂停(暂停)打印作业;

  • Write-PrinterNfcTag - 将信息写入 NFC 标签。

要获取有关任何命令语法的详细信息,请使用以下命令:

Get-Help <cmdlet_name> -Detailed

使用命令的示例:

Get-Help < cmdlet_name> -Examples

让我们看一下在 Windows 10 中使用 PowerShell 执行典型打印机管理任务的几个示例。

将打印机驱动程序添加到 DriverStore

要列出 Windows DriverStore 中安装的打印驱动程序:

Get-PrinterDriver

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

然后,在系统中安装新的打印机驱动程序。例如,您想要安装流行的打印驱动程序“HP Universal Printing PCL 6”。根据文档,添加打印驱动程序的PowerShell命令应该如下:

Add-PrinterDriver -Name "HP Universal Printing PCL 6" -InfPath "C:\Distr\HP-pcl6-x64\hpcu118u.inf"

但是,当尝试以这种方式安装驱动程序时,会出现以下错误消息:

Add-PrinterDriver : One or more specified parameters for this operation has an invalid value.At line:1 char:1+ Add-PrinterDriver -Name “HP Universal Printing PCL 6” -InfPath “C:\Di …+ ~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : InvalidArgument: (MSFT_PrinterDriver:ROOT/StandardCimv2/MSFT_PrinterDriver) [Add-PrinterDriver], CimException   + FullyQualifiedErrorId : HRESULT 0x80070057,Add-PrinterDriver

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

事实证明,只有当 DriverStore 中已存在 INF 文件中的驱动程序时,才能安装该驱动程序。您似乎无法使用 Add-PrinterDriver 命令安装不在驱动程序存储中的打印驱动程序。要将驱动程序添加到 DriverStore,您可以使用:

  • 上一篇文章中介绍的VBS脚本;

  • 该实用程序 - pnputil.exe。该命令如下所示:

    pnputil.exe -i -a C:\Distr\HP-pcl6-x64\hpcu118u.inf 

    (安装特定的打印机驱动程序)或

    pnputil.exe -i -a C:\Distr\HP-pcl6-x64\*.inf

    (安装指定目录中 INF 文件中找到的所有驱动程序);

    [玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

  • cmdlet Add-WindowsDriver 允许将驱动程序集成到脱机 Windows 映像中。

将打印机驱动程序添加到驱动程序存储库后,您应该将其安装在打印服务器上:

Add-PrinterDriver -Name "HP Universal Printing PCL 6"

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

提示。通过 PowerShell 安装驱动程序时,如何找出应在打印驱动程序名称字段中指定的内容?指定的打印驱动程序名称必须与其内部系统名称完全匹配,否则在驱动程序安装过程中会出现错误。您可以在已安装此打印驱动程序的计算机上使用 get-printerdriver 命令找到正确的驱动程序名称,或者手动检查驱动程序的 .inf 文件。

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

如何使用PowerShell安装打印机?

为网络打印机创建IP端口(这里可以指定网络打印机的IP地址和远程打印服务器的名称):

Add-PrinterPort -Name "IP_192.168.10.26" -PrinterHostAddress "192.168.10.26"

在添加新的IP打印端口之前,您可以检查它是否存在:

$portName = "IP_192.168.10.26"
$checkPortExists = Get-Printerport -Name $portname -ErrorAction SilentlyContinue
if (-not $checkPortExists) {
Add-PrinterPort -name $portName -PrinterHostAddress "192.168.10.26"
}

借助以下命令,我们将在计算机上安装并共享新打印机:

Add-Printer -Name hp3027_Office1_Buh -DriverName "HP LaserJet M3027 MFP PCL6 Class Driver" -PortName IP_192.168.10.26 -Shared -ShareName "hp3027_1_BUh" -Published

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

注意:请注意,要使用 VBS 脚本(打印管理脚本)执行相同的操作(安装和共享打印机),您应该执行两个不同的命令。

运行这些命令后,系统中将出现一个名为“hp3027_Office1”的新共享打印机。

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

要重命名打印机,只需运行以下命令:

Rename-Printer -Name "hp3027_1_Buh" -NewName "hp3027_F1_Salary"

列出打印服务器上安装的打印机

让我们显示此计算机上安装的打印机的完整列表:

Get-Printer

如您所见,该命令显示打印机名称、类型(本地或网络)、驱动程序、打印端口、打印机是否在 Active Directory 中共享和发布。

[玩转系统] 在 Windows 10/Server 2016 中使用 PowerShell 管理打印机和驱动程序

大多数 PrintManagement cmdlet 可用于查看状态并管理远程计算机(打印服务器)上的打印机、驱动程序和打印队列。远程计算机或服务器的名称指定为 -ComputerName 参数的参数。

您可以使用 PowerShell 命令获取有关远程计算机上已安装打印机的信息:

Get-Printer -ComputerName rome-prnt1 | Format-List Name,DriverName

要仅显示共享打印机的列表,请使用以下命令:

Get-Printer -ComputerName rome-prnt1 | where Shared -eq $true | fl Name

使用 PowerShell 连接到网络共享打印机

要从打印服务器连接共享打印机,请使用以下命令:

Add-Printer -ConnectionName \rome-prnt1\HP3027

Windows 10 使用用于打印的最新打印机作为默认打印机。如果您想使用固定的默认打印机,请运行以下命令:

Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Force

要设置默认打印机,可以使用以下命令:

$wsnObj = New-Object -COM WScript.Network
$wsnObj.SetDefaultPrinter(%PrinterName%)

如何使用 PowerShell 删除打印机?

要删除打印机,您需要运行以下 PowerShell 命令:

Remove-Printer -Name "hp3027_L1_O1"

您可以使用Remove-PrinterDriver cmdlet 删除特定驱动程序:

Remove-PrinterDriver -Name "HP Universal Printing PCL 6"

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯