Get-Printer并将输出设置为可用于设置默认打印机的变量

时间:2019-05-20 14:08:41

标签: powershell

我目前正在获得计算机连接到的所有打印机的列表。我希望打印机成为列表的一部分,然后用户可以选择将哪台打印机设置为默认打印机。然后它将执行我已经完成的脚本的下一部分,以创建一个文档,然后进行打印。

Get-Printer | Select PortName | Select-String -Pattern "SM"

这就是我希望代码返回的内容


1. SM04
2. SM05
3. SM06
4. SM10

Please select a printer from the list: 3

Default printer has been set to: SM06. 

任何帮助我们都将不胜感激。

3 个答案:

答案 0 :(得分:0)

我认为没有Powershell命令可以设置默认打印机。这就是我在WMI中的操作方法。令人惊讶的是,get-printer不会使用SetDefaultPrinter()方法返回对象。

$printerObject = Get-WmiObject win32_printer | where name -eq $defaultprinter
$resultobj = $printerObject.SetDefaultPrinter()
"default $defaultprinter $($resultobj.returnvalue)"  # showing results

答案 1 :(得分:0)

我将使用Get-WmiObject来获取/设置默认打印机,并且
Out-Gridview-PassThru参数在GUI中选择新的默认值

## Q:\Test\2019\05\20\SO_56222678.ps1
# Save old default Printer
$OldDefaultPrinter = (Get-WmiObject -Namespace root\cimv2 -Query `
    "select * from Win32_Printer Where Default = TRUE" -Impersonation 3).Name

# Get-Printer matching pattern and select in Out-Gridview
$NewDefaultPrinter = (Get-Printer | Where-Object Portname -like 'SM*'| 
  Select-Object Name,Portname |
    Out-Gridview -Title 'Select a printer' -PassThru).Name

# Set selected as new default
$Null = (Get-WmiObject -Class Win32_Printer -Filter "Name='$NewDefaultPrinter'").SetDefaultPrinter()

# Check new default is set
(Get-WmiObject -Namespace root\cimv2 -Query `
    "select * from Win32_Printer Where Default = TRUE" -Impersonation 3).Name

答案 2 :(得分:0)

感谢js2010和LotPings,我得以弄清楚。我什至可以得到计算机连接到的所有打印机的列表!

这里是代码,以防其他人尝试执行此类操作。

#Asks the user if they would like to switch the default printer
$defaultPrinter1 = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | select ShareName
$defaultPrinter1 = ("$defaultPrinter1").substring(12,4)
Write-Host "Barcodes will be printed at your default Printer. Your default printer is currently: $defaultPrinter1"
$printerConf = Read-Host "Would you like to change the default printer? Y/N"


#Switches dafault printer to the one specified by the user. 
if ($printerConf -eq "Y") 
{
    Write-Host "Here is list of connected printers`n" 
    $printerList = Get-Printer | Select PortName | Select-String -Pattern "SM" 
    $printerList-replace ".*=" -replace "}.*"
    $wshNet = New-Object -ComObject WScript.Network
    $selectPrinter = Read-Host "`nWhat is the printers name?"
    $wshNet.SetDefaultPrinter($selectPrinter)

    $defaultPrinter2 = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | select ShareName 
    $defaultPrinter2 = ("$defaultPrinter2").substring(12,4)  
    Write-Host "Default Printer has been changed. It is now: $defaultPrinter2"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
}