John Ranger
John Ranger

Reputation: 633

Powershell; foreach-object; How to create proper output again to pipeline

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

Upvotes: 2

Views: 991

Answers (2)

Esperento57
Esperento57

Reputation: 17492

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
    }
}

Upvotes: 4

Vincent K
Vincent K

Reputation: 1346

Create Custom property using Select-Object cmdlet.

Get-WmiObject -Class Win32_PrinterDriver | select Name,Driverpath,@{l='Version';ex={(get-item -LiteralPath $_.driverpath).VersionInfo.ProductVersion}}

Upvotes: 5

Related Questions