none
powershell wmi printer and pnp export to csv RRS feed

  • Question

  • hello i am trying to export to csv; all of the devices found on multiple computers using powershell and wmi, when trying to capture either printer data or pnp, i am only able to loop 1 data value to export to my csv. here is a little snippet, right now i am just trying to gather pnp data.

    $computers = Get-Content -Path C:\machines.txt
    $computers | foreach-object {	
    $PrintInfos = get-wmiobject Win32_PnPEntity -cn $_
      ForEach ($PrintInfo in $PrintInfos)
        {		
    $props=[ordered]@{ 
    "ComputerName" = $_
    "DeviceID" = $PrintInfo.Name
        }
    }		
    new-object PSObject -property $props #
      } | Sort ComputerName | Export-csv C:\Users\Public\outputfile.csv -notypeinformation

    Thank you for the help.
    Friday, July 18, 2014 6:34 PM

Answers

  • Hi,

    Here's how I'd go about this:

    Get-Content .\computerList.txt | ForEach {
    
        $computerName = $_
    
        Get-WmiObject Win32_PnPEntity -ComputerName $computerName | ForEach {
    
            $props = [ordered]@{
                ComputerName = $computerName
                DeviceID = $_.Name
            }
    
            New-Object PsObject -Property $props
    
        }
    
    } | Sort ComputerName,DeviceID | Export-Csv .\outfile.csv -NoTypeInformation


    This uses ForEach-Object instead of foreach.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by justin rassi Friday, July 18, 2014 7:08 PM
    Friday, July 18, 2014 6:51 PM

All replies