Powershell - Get list of Print Drivers

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
 }
myhelpermonkeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prashant GirennavarCommented:
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
footechCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.