0

I'm a little newbie in powershell and I need your help with this problem.

I need to Get-PrinterConfiguration and Get-Printer to work together.

I found this code:

$Printers = Get-Printer *
Foreach ($Printer in $Printers){Get-PrintConfiguration -PrinterName $Printer.name}

But I still need some info that Get-Printer has, but I don't know how to make it work all together.

For instance I need:

Name, DriverName, PortName, Comment, Location from Get-Printer and Color, DuplexingMode from Get-PrintConfiguration.

May you guys help me with this?

2
  • [1] please add formatting markers around your code. the how-to is linked on the page you used to create your Question. ///// [2] have you looked at the properties of any of the items in your $Printers collection? try using Get-Member ... [grin]
    – Lee_Dailey
    Commented Mar 9, 2021 at 19:35
  • As for this "I'm a little newbie in PowerShell". that is all well and good as we've all been there, past, present, and future, but this does mean, at minimum, you should spend the time getting up to speed on the topic. As you asked is a Day one, hour one, cmdlet use case, with full documentation, samples/examples in the built-in Powershell help files. As well as all over the web, and via Youtube videos. Don't stress always know that virtually any IT Pro question you have, has already been asked/answered many times/many ways on the web and other references long before you'd hit SU/SO, etc.
    – postanote
    Commented Mar 9, 2021 at 20:44

2 Answers 2

2

This...

$Printers = Get-Printer *
Foreach ($Printer in $Printers){Get-PrintConfiguration -PrinterName $Printer.name}

... can be simplified to this...

Get-Printer | 
Foreach {Get-PrintConfiguration -PrinterName $PSItem.name}

Use the help file to know what is/is not available before you begin

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-Printer).Parameters
(Get-Command -Name Get-Printer).Parameters.Keys
# Results
<#
Name
ComputerName
Full
CimSession
ThrottleLimit
AsJob
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>
Get-help -Name Get-Printer -Examples
Get-help -Name Get-Printer -Full
Get-help -Name Get-Printer -Online


(Get-Command -Name Get-PrintConfiguration).Parameters
(Get-Command -Name Get-PrintConfiguration).Parameters.Keys
# Results
<#
ComputerName
PrinterName
PrinterObject
CimSession
ThrottleLimit
AsJob
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>
Get-help -Name Get-PrintConfiguration -Examples
Get-help -Name Get-PrintConfiguration -Full
Get-help -Name Get-PrintConfiguration -Online


# Get all data about one printer
Get-Printer | 
Select-Object -First 1 | 
Select-Object -Property '*'
# Results
<#
RenderingMode                : 
PrinterStatus                : Normal
Type                         : Local
DeviceType                   : Print
Caption                      : 
Description                  : 
ElementName                  : 
InstanceID                   : 
CommunicationStatus          : 
DetailedStatus               : 
HealthState                  : 
InstallDate                  : 
Name                         : OneNote for Windows 10
OperatingStatus              : 
OperationalStatus            : 
PrimaryStatus                : 
Status                       : 
StatusDescriptions           : 
BranchOfficeOfflineLogSizeMB : 
Comment                      : 
ComputerName                 : 
Datatype                     : RAW
DefaultJobPriority           : 0
DisableBranchOfficeLogging   : 
DriverName                   : Microsoft Software Printer Driver
JobCount                     : 0
KeepPrintedJobs              : False
Location                     : 
PermissionSDDL               : 
PortName                     : Microsoft.Office.OneNote_1600...
PrintProcessor               : winprint
Priority                     : 1
Published                    : False
SeparatorPageFile            : 
Shared                       : False
ShareName                    : 
StartTime                    : 0
UntilTime                    : 0
WorkflowPolicy               : 
PSComputerName               : 
CimClass                     : ROOT/StandardCimv2:MSFT_Printer
CimInstanceProperties        : {Caption...
CimSystemProperties          : Microsoft.Management.Infrastructure.CimSystemProperties
#>


Get-Printer | 
Select-Object -First 1 | 
Foreach {
    Get-PrintConfiguration -PrinterName $PSItem.name | 
    Select-Object -Property '*'
}
# Results
<#
DuplexingMode         : OneSided
PaperSize             : Letter
Collate               : True
Color                 : True
ComputerName          : 
PrintCapabilitiesXML  : <?xml version="1.0"?>
                        <...
                    
PrinterName           : OneNote for Windows 10
PrintTicketXML        : <?xml version="1.0"?>
                        ...
                    
PSComputerName        : 
CimClass              : ROOT/StandardCimv2:MSFT_PrinterConfiguration
CimInstanceProperties : {Collate, Color, ComputerName, DuplexingMode, PaperSize, PrintCapabilitiesXML, PrinterName, PrintTicketXML}
CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties
#>

You need to select the stuff you want, then combine together using a hash-table or custom object. Now that is not day one stuff, but again, many articles/blogs/videos, samples/examples on combining cmdlet output is plentiful.

PowerShell combine cmdlet output

https://duckduckgo.com/?q=PowerShell+combine+cmdlet+output&t=h_&ia=web

Example:

Get-Printer | 
Select-Object -First 1 | 
Select-Object -Property Name, DriverName, PortName, Comment, Location, 
@{
    Name       = 'Color'
    Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).Color}
},
@{
    Name       = 'DuplexingMode '
    Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).DuplexingMode }
}
# Results
<#
Name           : OneNote for Windows 10
DriverName     : Microsoft Software Printer Driver
PortName       : Microsoft.Office.OneNote_16001.13801.20202.0_x64__8wekyb3d8bbwe_microsoft.onenoteim_S-1-5-21-3258886415-1034932420-179337933-1002
Comment        : 
Location       : 
Color          : True
DuplexingMode  : OneSided
#>

To get all your printers, remove this line...

Select-Object -First 1 | 

... no ForLoop required for your use case.

The above could also be written this way if you want to compress the number of lines.

Get-Printer | Select-Object -Property Name, DriverName, PortName, Comment, Location, 
@{Name = 'Color';Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).Color}},
@{Name = 'DuplexingMode';Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).DuplexingMode}}
# Results
<#
Name          : OneNote for Windows 10
DriverName    : Microsoft Software Printer Driver
PortName      : Microsoft.Office.OneNote_16001.13801.20202.0_x64__8wekyb3d8bbwe_microsoft.onenoteim_S-1-5-21-3258886415-1034932420-179337933-1002
Comment       : 
Location      : 
Color         : True
DuplexingMode : OneSided

...

Name          : Microsoft XPS Document Writer
DriverName    : Microsoft XPS Document Writer v4
PortName      : PORTPROMPT:
Comment       : 
Location      : 
Color         : True
DuplexingMode : OneSided

Name          : Microsoft Print to PDF
DriverName    : Microsoft Print To PDF
PortName      : PORTPROMPT:
Comment       : 
Location      : 
Color         : True
DuplexingMode : OneSided

Name          : Fax
DriverName    : Microsoft Shared Fax Driver
PortName      : SHRFAX:
Comment       : 
Location      : 
Color         : False
DuplexingMode : OneSided

...
#>

Yet, I just find that harder to read. But that is just me. The use of natural line breaks, like '|', ';', ::', comparison operators and others just allow for making things more humanly readable.

2
  • Thank you so much for your help and guidance! I'll do what you recommended and improve my powershell skills This works like charm! Commented Mar 9, 2021 at 21:36
  • No worries, we all do what we can here. Be sure to mark one of the submitted responses as your accepted answer, for the benefit of others who visit here and find the same use case.
    – postanote
    Commented Mar 9, 2021 at 21:39
1

Here is a script that demonstrates how to get properties from multiple classes and unify them all in one new object.

I leave you to learn and understand the commands used and adapt the script to your needs.

$Printers = Get-Printer *
Foreach ($Printer in $Printers){
$x = new-object -typename psobject 
$x | Add-Member -Type NoteProperty -Name PrinterName -Value $Printer.name
$x | Add-Member -Type NoteProperty -Name DriverName -Value $Printer.DriverName
$x | Add-Member -Type NoteProperty -Name PortName -Value $Printer.PortName
$x | Add-Member -Type NoteProperty -Name Comment -Value $Printer.Comment
$x | Add-Member -Type NoteProperty -Name Location -Value $Printer.Location
$config = Get-PrintConfiguration -PrinterName $Printer.name
$x | Add-Member -Type NoteProperty -Name Color -Value $config.Color
$x | Add-Member -Type NoteProperty -Name DuplexingMode -Value $config.DuplexingMode
Write-Output $x
}
1
  • Thank you! This also worked like I needed! I'll do my best to improve my skills this will lead me the way. Thanks again Commented Mar 9, 2021 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .