[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell - Get list of Print Drivers

Posted on 2014-08-07
2
Medium Priority
?
1,713 Views
Last Modified: 2014-08-08
In Powershell, I need to produce a list of the printer drivers installed on a number of different servers.  I found this script online but it does not seem to be working correctly.  If possible, I need it to output the report to a file, such as a csv.  Any help you can provide would be much appreciated.


# Set name of PC to query
 $strComputer = gc c:\temp\MrCleanServerList.txt
 
# Create an array of items from Win32_Printer class in WMI
 $colItems = get-wmiobject -class "Win32_Printer" -namespace "root\CIMV2" -computername $strComputer
 
# Enumerate through the Win32_Printer array and print out properties for each Printer Object
 foreach ($objItem in $colItems)
 {
 Write-Host "Name: " $objItem.Name
 $MyVar0=$objItem.GetRelated('Cim_Datafile')|Select Name
 $MyVar1=$objItem.GetRelated('Cim_Datafile')|Select Version
 $MyVar2=$objItem.GetRelated('Cim_Datafile')|Select Caption
 $MyVar3=$objItem.GetRelated('Cim_Datafile')|Select PSComputerName
 # $MyVar4=$objItem.GetRelated('Cim_Datafile')|Select *
 Write-Host "Name: " $MyVar0
 Write-Host "Version: " $MyVar1
 Write-Host "Caption: " $MyVar2
 Write-Host "Computer Name: " $MyVar3
 # $MyVar4
 Write-Host "----------------------------------------------------------------------------------------"
 Write-Host
 }
0
Comment
Question by:myhelpermonkey
2 Comments
 
LVL 10

Assisted Solution

by:Prashant Girennavar
Prashant Girennavar earned 1000 total points
ID: 40248000
Try below code.

# Set name of PC to query
$strComputer = "."

# Create an array of items from Win32_Printer class in WMI
$colItems = get-wmiobject -class "Win32_Printer" -namespace "root\CIMV2" -computername $strComputer

# Enumerate through the Win32_Printer array and print out properties for each Printer Object

$Result=@()
foreach ($objItem in $colItems)
{
$object = New-Object PSObject

$m=$objItem.GetRelated('Cim_Datafile')|Select Name
Add-Member -InputObject $object -memberType NoteProperty -Name Name1 -Value $m
$n=$objItem.GetRelated('Cim_Datafile')|Select Version
Add-Member -InputObject $object -memberType NoteProperty -Name Version1 -Value $n
$o=$objItem.GetRelated('Cim_Datafile')|Select Caption
Add-Member -InputObject $object -memberType NoteProperty -Name Caption1 -Value $o
$p=$objItem.GetRelated('Cim_Datafile')|Select __Server
Add-Member -InputObject $object -memberType NoteProperty -Name ComputerName1 -Value $p
$q=$objItem.GetRelated('Cim_Datafile')|Select *
Add-Member -InputObject $object -memberType NoteProperty -Name random1 -Value $q
$result+=$Object
}
$Result | export-csv C:\PrinterReport.csv -NotypeInformation

Open in new window


Let me know if you need more help.

Thanks,

-Prashant Girennavar.
0
 
LVL 41

Accepted Solution

by:
footech earned 1000 total points
ID: 40248028
Well, it works from my testing, just that things aren't formatted real well, and the version may not be what you want.  Also, it will only output to screen and not to file.  Here's a modification that will work better.
Get-Content c:\temp\ComputerList.txt | ForEach `
{
    Get-WmiObject -class "Win32_Printer" -namespace "root\CIMV2" -computername $comp | ForEach `
    {
        $printer = $_.Name
        $_.GetRelated('Cim_Datafile') | Select @{n="Computer";e={$_.PSComputerName}},
                                        @{n="Printer";e={$printer}},
                                        @{n="Driver";e={$_.Name}},
                                        @{n="Version";e={$Drive = $_.Name.Substring(0,1); (Get-ItemProperty ($_.Name.Replace("$Drive`:","\\$($_.PSComputerName)\$Drive`$"))).VersionInfo.ProductVersion }}
    }
} | Export-CSV printerinfo.csv -notype

Open in new window

0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question