2

I have already searched quite a bit to find a satisfying answer to this but could not find it (neither here on SO nor via Google).

I am currently using the following command to get the Name and the Product Version out of all Printer Drivers on a print Server and pipe it to the pipeline:

Get-WmiObject -Class Win32_PrinterDriver | select name, driverpath | %{ $Name = $_.name; $version = (get-item -LiteralPath $_.driverpath).VersionInfo.ProductVersion; write-output ($Name, $Version) }

Technically, this works but the piped Output is ugly. The Output Looks like this:

Remote Desktop Easy Print,3,Windows x64
6.1.7601.17514
PCL6 Driver for Universal Print,3,Windows x64
3.4.100.1
MS Publisher Color Printer,3,Windows x64
6.1.7600.16385

What I want to achieve is something like the following (like the Standard Output of objects which are not piped through the foreach-object) - eg. from the following command:

Get-WmiObject -Class Win32_PrinterDriver | select name, driverpath

The Output of the above command Looks like this:

name                                                        driverpath
----                                                        ----------

Remote Desktop Easy Print,3,Windows x64                     C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
PCL6 Driver for Universal Print,3,Windows x64               C:\Windows\system32\spool\DRIVERS\x64\3\ricu00gr.dll

Question: How can I prepare the Output within a foreach-object to resemble the above formatting / object piping?

Thanks for any Input.

With Kind regards

2 Answers 2

5

Create Custom property using Select-Object cmdlet.

Get-WmiObject -Class Win32_PrinterDriver | select Name,Driverpath,@{l='Version';ex={(get-item -LiteralPath $_.driverpath).VersionInfo.ProductVersion}}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. Exactly what I was looking for. Thanks for such a fast Reply.
4

You can create a pscustomobject into a foreach loop like this :

Get-WmiObject -Class Win32_PrinterDriver | %{
    [pscustomobject]@{
    Name=$_.Name
    Driverpath=$_.Driverpath
    Version=(Get-Item $_.Driverpath).VersionInfo.ProductVersion
    }
}

1 Comment

Thanks for your answer. Technically, this is the direct answer to my question (how to create proper Output to pipeline from inside a foreach.object loop. The answer from Vincent K was the exact match to solve the Problem I had in a very elegant manner. But because your answer Matches the question, I give you the "accepted answer flag".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.