<#
.SYNOPSIS
自动设置 Windows 默认打印选项 (如双面打印、黑白、纸张大小等)
.DESCRIPTION
通过修改注册表,为当前用户设置打印机的默认打印偏好。
需要以管理员权限运行以修改所有用户配置,或以普通用户身份仅修改当前用户。
.NOTES
作者: AI 助手
版本: 1.0
注意: 请根据实际打印机名称和需要的参数修改脚本中的变量。
#>
# ========== 配置区域 - 请根据您的需要修改 ==========
# 目标打印机名称 (在“设备和打印机”中看到的名称)
$PrinterName = "Microsoft Print to PDF" # 示例: "HP LaserJet Pro M404n" 或 "Microsoft Print to PDF"
# 打印参数设置
$PaperSize = "A4" # 纸张大小: A4, Letter, Legal 等
$Duplex = "DuplexHorizontal" # 双面打印: "DuplexHorizontal" (长边翻转), "DuplexVertical" (短边翻转), "Simplex" (单面)
$Color = "Mono" # 颜色: "Color" (彩色) 或 "Mono" (黑白/灰度)
$Collate = "True" # 逐份打印: "True" 或 "False"
$Copies = 1 # 打印份数
$Orientation = "Portrait" # 方向: "Portrait" (纵向) 或 "Landscape" (横向)
$Resolution = "600" # 默认分辨率 (DPI), 如 "600", "1200" (并非所有打印机支持)
# ========== 以下为脚本逻辑 - 通常无需修改 ==========
# 检查是否以管理员权限运行 (可选)
# 如果需要修改系统范围的打印机设置,可能需要管理员权限;修改当前用户设置通常不需要。
# if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# Write-Host "错误: 请以管理员身份运行此脚本。" -ForegroundColor Red
# exit 1
# }
# 函数:设置默认打印参数
function Set-PrinterDefaults {
param (
[string]$PrinterName,
[string]$PaperSize,
[string]$Duplex,
[string]$Color,
[string]$Collate,
[int]$Copies,
[string]$Orientation,
[string]$Resolution
)
# 使用 WMI 获取打印机对象
$printer = Get-WmiObject -Class Win32_Printer -Filter "Name='$PrinterName'"
if (-not $printer) {
Write-Host "错误: 找不到打印机 '$PrinterName',请检查打印机名称。" -ForegroundColor Red
return $false
}
Write-Host "正在配置打印机: $PrinterName" -ForegroundColor Cyan
try {
# 获取打印机默认设置 (DevMode)
$devMode = $printer.GetDeviceContext() # 尝试获取当前设备上下文
# 注意: Win32_Printer 的 GetDeviceContext 可能不总是可用,因此我们采用另一种更通用的方法
# 另一种方法: 通过注册表直接修改
# 注册表路径: HKCU:\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\
# 或 HKLM:\SYSTEM\CurrentControlSet\Control\Print\Printers\ 下的 PrtMfgInfo 等
# 更可靠的方案: 使用 PowerShell 的 Set-PrintConfiguration (Windows 8+ / Server 2012+)
# 但 Set-PrintConfiguration 只能设置部分选项,且需要打印机支持
Write-Host "尝试使用 Set-PrintConfiguration 设置选项..." -ForegroundColor Yellow
# 注意: 以下命令可能需要管理员权限,并且部分打印机驱动可能不兼容
$printConfig = @{
PrinterName = $PrinterName
# PaperSize 参数需要具体名称,可能因驱动而异
}
# 由于 Set-PrintConfiguration 的局限性,我们改用更底层的 DevMode 修改方法
# 通过注册表修改默认 DevMode
Write-Host "通过注册表修改默认打印设置..." -ForegroundColor Yellow
# 获取打印机注册表路径 (当前用户)
$regPath = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\$PrinterName"
# 检查路径是否存在 - 如果不存在,可能打印机从未被设置为默认或使用过
if (-not (Test-Path $regPath)) {
Write-Host "警告: 未找到用户打印机配置路径,尝试创建..." -ForegroundColor Yellow
# 有时需要在 HKCU 下先创建一项
$null = New-Item -Path $regPath -Force -ErrorAction SilentlyContinue
}
# 读取默认的 DevMode 数据 (Default DevMode)
# 注意: DevMode 是二进制数据,直接修改较为复杂。
# 我们选择更简单的方法: 使用 PrintUIEntry 命令 (Windows 自带)
Write-Host "使用 rundll32 printui.dll 命令配置默认选项..." -ForegroundColor Green
# 构建命令行参数
# 注意: printui.dll 的语法较为复杂且有限,不同语言/版本可能有差异
# 我们采用更现代的 PrintManagement 模块 (如果可用)
# 方案A: 使用 PrintManagement 模块 (Windows 10/11 专业版/企业版)
if (Get-Command -Name Set-PrintConfiguration -ErrorAction SilentlyContinue) {
Write-Host "发现 Set-PrintConfiguration cmdlet,尝试直接设置..." -ForegroundColor Green
$params = @{}
$params.PrinterName = $PrinterName
# 纸张大小 (需匹配驱动支持)
if ($PaperSize -eq "A4") { $params.PaperSize = "A4" }
elseif ($PaperSize -eq "Letter") { $params.PaperSize = "Letter" }
# 双面打印
if ($Duplex -eq "DuplexHorizontal") { $params.DuplexingMode = "TwoSidedLongEdge" }
elseif ($Duplex -eq "DuplexVertical") { $params.DuplexingMode = "TwoSidedShortEdge" }
elseif ($Duplex -eq "Simplex") { $params.DuplexingMode = "OneSided" }
# 颜色
if ($Color -eq "Mono") { $params.Color = $false } # 假设 false 表示黑白
elseif ($Color -eq "Color") { $params.Color = $true }
# 逐份打印
if ($Collate -eq "True") { $params.Collate = $true }
else { $params.Collate = $false }
# 份数
$params.CopyCount = $Copies
# 方向
if ($Orientation -eq "Portrait") { $params.PaperOrientation = "Portrait" }
elseif ($Orientation -eq "Landscape") { $params.PaperOrientation = "Landscape" }
# 分辨率 (并非所有驱动支持)
# $params.Resolution = $Resolution # 可能不支持直接设置
try {
Set-PrintConfiguration @params -ErrorAction Stop
Write-Host " 成功通过 Set-PrintConfiguration 设置打印选项。" -ForegroundColor Green
return $true
}
catch {
Write-Host " 使用 Set-PrintConfiguration 失败: $_" -ForegroundColor Yellow
Write-Host " 回退到 printui.dll 方法..." -ForegroundColor Yellow
}
}
# 方案B: 使用 printui.dll 命令 (兼容性更好)
Write-Host "尝试 printui.dll 命令行方法..." -ForegroundColor Yellow
# 构建参数: /Sr 参数可以指定要设置的选项
# 格式: rundll32 printui.dll,PrintUIEntry /Sr [flags] [options] /n "printer name"
# 但这需要知道具体的 flags 组合,且不直观
# 我们采用更通用的方法: 使用 .NET 直接修改打印机 DevMode
Write-Host "尝试使用 .NET 修改打印机默认设置..." -ForegroundColor Yellow
Add-Type -AssemblyName System.Printing
try {
$printServer = New-Object System.Printing.LocalPrintServer
$printQueue = $printServer.GetPrintQueue($PrinterName)
if ($printQueue -eq $null) {
Write-Host " 无法获取打印队列。" -ForegroundColor Red
return $false
}
# 获取默认打印工单
$printTicket = $printQueue.DefaultPrintTicket
# 修改属性
# 纸张大小
if ($PaperSize -eq "A4") { $printTicket.PageMediaSizeName = [System.Printing.PageMediaSizeName]::ISOA4 }
elseif ($PaperSize -eq "Letter") { $printTicket.PageMediaSizeName = [System.Printing.PageMediaSizeName]::Letter }
# 双面打印
if ($Duplex -eq "DuplexHorizontal") { $printTicket.Duplexing = [System.Printing.Duplexing]::TwoSidedLongEdge }
elseif ($Duplex -eq "DuplexVertical") { $printTicket.Duplexing = [System.Printing.Duplexing]::TwoSidedShortEdge }
elseif ($Duplex -eq "Simplex") { $printTicket.Duplexing = [System.Printing.Duplexing]::OneSided }
# 颜色 (可能不支持所有打印机)
if ($Color -eq "Mono") { $printTicket.OutputColor = [System.Printing.OutputColor]::Grayscale }
elseif ($Color -eq "Color") { $printTicket.OutputColor = [System.Printing.OutputColor]::Color }
# 逐份打印
$printTicket.Collation = if ($Collate -eq "True") { [System.Printing.Collation]::Collated } else { [System.Printing.Collation]::Uncollated }
# 副本数
$printTicket.CopyCount = $Copies
# 方向
if ($Orientation -eq "Portrait") { $printTicket.PageOrientation = [System.Printing.PageOrientation]::Portrait }
else { $printTicket.PageOrientation = [System.Printing.PageOrientation]::Landscape }
# 应用设置
$printQueue.DefaultPrintTicket = $printTicket
$printQueue.Commit()
Write-Host " 成功通过 .NET 设置默认打印选项。" -ForegroundColor Green
$printServer.Dispose()
return $true
}
catch {
Write-Host " .NET 方法失败: $_" -ForegroundColor Yellow
Write-Host " 尝试最终的注册表方法..." -ForegroundColor Yellow
}
# 方案C: 直接修改注册表 DevMode (最后手段)
# 注意: DevMode 是二进制结构,修改复杂,此处提供思路但不深入实现
Write-Host " 注册表直接修改 DevMode 较为复杂,请考虑使用专业工具。" -ForegroundColor Red
Write-Host " 推荐使用 Set-PrintConfiguration 或打印机属性对话框手动配置。" -ForegroundColor Cyan
return $false
}
catch {
Write-Host "错误: 设置打印选项时出现异常: $_" -ForegroundColor Red
return $false
}
}
# 执行主函数
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 自动设置默认打印选项脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "目标打印机: $PrinterName" -ForegroundColor White
Write-Host "纸张大小 : $PaperSize" -ForegroundColor White
Write-Host "双面打印 : $Duplex" -ForegroundColor White
Write-Host "颜色模式 : $Color" -ForegroundColor White
Write-Host "========================================" -ForegroundColor Cyan
$result = Set-PrinterDefaults -PrinterName $PrinterName -PaperSize $PaperSize -Duplex $Duplex -Color $Color -Collate $Collate -Copies $Copies -Orientation $Orientation -Resolution $Resolution
if ($result) {
Write-Host "`n设置完成!请打开任意文档进行打印测试以验证效果。" -ForegroundColor Green
} else {
Write-Host "`n设置未完全成功,请尝试手动配置打印机默认选项。" -ForegroundColor Yellow
Write-Host "手动步骤: 设置 -> 蓝牙和设备 -> 打印机和扫描仪 -> 选择打印机 -> 打印首选项" -ForegroundColor Cyan
}
# 可选: 列出当前打印机的配置
Write-Host "`n当前打印机默认配置摘要:" -ForegroundColor Magenta
try {
$currentConfig = Get-PrintConfiguration -PrinterName $PrinterName -ErrorAction SilentlyContinue
if ($currentConfig) {
$currentConfig | Format-List PrinterName, PaperSize, DuplexingMode, Color, Collate, CopyCount, PaperOrientation
}
} catch {
Write-Host "无法获取当前配置 (可能需要管理员权限)" -ForegroundColor DarkGray
}
# 暂停,以便查看结果
Read-Host "`n按 Enter 键退出..."
脚本使用说明
-
修改配置区域:在脚本顶部的“配置区域”中,根据您的打印机和需求修改变量。

$PrinterName:请替换为您的实际打印机名称(可在“设置 > 蓝牙和设备 > 打印机和扫描仪”中查看)。- 其他参数如
$PaperSize、$Duplex等,请根据需要调整,注意部分打印机驱动可能不支持所有选项。
-
运行脚本:
- 将脚本保存为
.ps1文件(Set-PrintDefaults.ps1)。 - 右键点击文件,选择“使用 PowerShell 运行”。
- 如果遇到执行策略限制,请以管理员身份打开 PowerShell 并运行
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser。
- 将脚本保存为
-
脚本工作原理:
- 脚本会依次尝试三种方法设置默认打印选项:
Set-PrintConfigurationcmdlet、.NETSystem.Printing库、以及注册表修改。 - 通常前两种方法能适用于大多数现代打印机,运行结束后会显示是否成功及应用了哪些配置。
- 脚本会依次尝试三种方法设置默认打印选项:
-
注意事项:
- 部分打印机驱动可能不支持通过脚本修改所有选项,如果脚本执行后设置未生效,请手动在“打印首选项”中确认。
- 脚本默认仅为当前用户设置,如需为所有用户设置,请以管理员身份运行,并可能需要修改注册表路径为
HKLM。
希望这个脚本能帮助您提高打印效率,如果遇到特定打印机名称或选项问题,可以调整配置区域后重试。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播