Unable to export the correct csv Data

Mike 246 Reputation points
2022-03-20T06:48:40.953+00:00

I have this code the works fine but because it takes so long to query it, I made some modifications.

This is the working code:

$Devices = Get-MsolDevice -All -ReturnRegisteredOwners -IncludeSystemManagedDevices
$DeviceInfo = @()

foreach ($Device in $Devices) {
$DeviceInfo += [PSCustomObject]@{
"DisplayName" = $Device.DisplayName
"DeviceTrustType" = $Device.DeviceTrustType
"DeviceTrustLevel" = $Device.DeviceTrustLevel
"DeviceOS" = $Device.DeviceOsType
"DeviceVersion" = $Device.DeviceOsVersion
"RegisteredOwner" = $($Device.RegisteredOwners)
"LastLogon" = $Device.ApproximateLastLogonTimestamp
"LastDirSync" = $Device.LastDirSyncTime
"DeviceID" = $Device.DeviceId
"ObjectID" = $Device.ObjectId
}
}

This is the modified code, in which I have another csv $allobjects that contains objectID of Devices in Windows. By doing this, I have cut the amount of devices I need to query into half If I use the code above.

$a = @()
ForEach ($line in $allobjects)
{
$device = Get-MsolDevice -ObjectID $line.ObjectId | ? {$_.deviceos -eq "Windows"} | Select DisplayName,DeviceTrustType,ApproximateLastLogonTimestamp,RegisteredOwners
$a += New-Object psobject -property @{
"DisplayName" = $device.displayname
"DeviceTrustType" = $device.devicetrusttype
"ApproximateLastLogon" = $device.ApproximateLastLogonTimestamp
"RegisteredOwner" = $($Device.RegisteredOwners)}
}
$a | export-csv "C:\temp\alldevices.csv" -noTypeInformation

However, I can't seem to get the CSV right.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,547 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,666 Reputation points
    2022-03-28T10:57:50.333+00:00

    Hi @Mike

    It would be great if you share what is your exact intention on getting the result in the CSV as output.

    For certain types of properties, Windows PowerShell does not display all the information.

    One of the most common CSV export errors in PowerShell is that the file is simply too large. That can be caused by too many fields or records in the file, too many columns, or too many rows.

    See Why PowerShell Can't Export Some Properties to CSV
    https://devblogs.microsoft.com/scripting/see-why-powershell-cant-export-some-properties-to-csv/

    Hope this resolves your Query!!

    --
    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.