Exporting 2 list of devices from intune into 1 csv

Hi,
I am running a report in powershell to get devices in 2 different variables
1= all device pending wipe = $staledevices
2nd =all devices that need to be deleted $deletedevices

i want to combine those 2 list of devices into 1 csv write now it just out puts on screen

Hi James, welcome back. That sounds really useful. What’s stopping you?

1 Like

When I add ** |export-csv -path “C:\filename $stabledevices + $Deleteddevices.CSV”

i get this error

“System.Char”. Error: “String must be exactly one character long.”
At line:1 char:28

  • export-csv -Path “C:\temp"$pendingdevices” + “$staledevices”.csv"
  •                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Export-Csv], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Hi James. First things first, when sharing any kind of code (even error text) it’s helpful to use the </> “Preformatted Text” button and place your code between the backticks, like this:
image
so that the forum will render it like this:

Export-Csv

With that being said, I see at least one syntax error for sure that might be part of the problem, but first I don’t know what “** |” means at the beginning of your example. What are the double asterisks representing?

Going off some assumptions here’s what I think maybe you should be trying:

$StaleDevices,$DeletedDevices | Export-Csv -Path "C:\MyReport.csv" -NoTypeInformation

Let’s talk through this specifically using “object” language since Powershell is an object-oriented language. $StaleDevices and $DeletedDevices are variables, and each one is likely an Array object containing objects with several properties. Export-Csv is great with objects that have multiple properties because it creates a header row based on all of the shared property names, and then prints the values for each object in the appropriate columns.
In the above example we’re assuming that both arrays contain objects with identical properties. By calling both variables separated by a comma we’re just telling Powershell to send all of the objects to the pipeline to be handled by Export-Csv. We provide a destination filename for Export-Csv and also importantly we use the “NoTypeInformation” switch parameter which tells Export-Csv not to print the object type as the first row in the CSV file.

1 Like

I found the post interesting because I never thought of trying to export 2 sets of arrays at once so I tried it out. On this query I dont get the results expected. See Below:


$StaleDevices = Get-service | Where{$_.Name -like "R*"}

$DeletedDevices = Get-Service | Where{$_.Name -like "T*"}

$StaleDevices,$DeletedDevices | Export-Csv -Path "C:\Temp\MyReport.csv" -NoTypeInformation

I exported the Objects using the -append and got the desired results:

$StaleDevices = Get-service | Where{$_.Name -like "R*"}

$DeletedDevices = Get-Service | Where{$_.Name -like "T*"}

$DeletedDevices | Export-Csv -Path "C:\Temp\MyReport.csv"-NoTypeInformation

$StaleDevices  | Export-Csv -Path "C:\Temp\MyReport.csv" -Append -NoTypeInformation
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.