How to configure output to .csv?

%3CLINGO-SUB%20id%3D%22lingo-sub-2892918%22%20slang%3D%22en-US%22%3EHow%20to%20configure%20output%20to%20.csv%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2892918%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20all%2C%3C%2FP%3E%3CP%3EI'm%20new%20to%20using%20Powershell.%20I%20found%20this%20script%20online%20to%20help%20display%20who%20has%20a%20mobile%20device%20connected%20to%20O365.%20The%20script%20runs%20fine%20in%20PS%2C%20but%20I%20would%20like%20to%20output%20the%20results%20to%20.csv%20and%20I%20am%20not%20sure%20how%20to%20do%20that.%20I%20tried%20editing%20another%20script%20I%20found%20online%2C%20but%20no%20success.%20Any%20help%20is%20greatly%20appreciated.%20Here%20is%20what%20I%20am%20currently%20using.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24credentials%20%3D%20Get-Credential%20-Credential%20blank%40blank.onmicrosoft.com%3CBR%20%2F%3EWrite-Output%20%22Getting%20the%20Exchange%20Online%20cmdlets%22%3CBR%20%2F%3E%3CBR%20%2F%3E%24session%20%3D%20New-PSSession%20-ConnectionUri%20%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%20%60%3CBR%20%2F%3E-ConfigurationName%20Microsoft.Exchange%20-Credential%20%24credentials%20%60%3CBR%20%2F%3E-Authentication%20Basic%20-AllowRedirection%3CBR%20%2F%3EImport-PSSession%20%24session%3CBR%20%2F%3E%3CBR%20%2F%3E%24csv%20%3D%20%22C%3A%5Ctemp%5CMobileDevicesStatistics.csv%22%3CBR%20%2F%3E%24results%20%3D%20%40()%3CBR%20%2F%3E%24mailboxUsers%20%3D%20get-mailbox%20-resultsize%20unlimited%3CBR%20%2F%3E%24mobileDevice%20%3D%20%40()%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach(%24user%20in%20%24mailboxUsers)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24UPN%20%3D%20%24user.UserPrincipalName%3CBR%20%2F%3E%24displayName%20%3D%20%24user.DisplayName%3CBR%20%2F%3E%3CBR%20%2F%3E%24mobileDeviceList%20%3D%20Get-MobileDevice%20-Mailbox%20%24UPN%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach%20(%24Device%20in%20%24MobileDeviceList)%20%7B%3CBR%20%2F%3E%24Stats%20%3D%20Get-MobileDeviceStatistics%20-Identity%20%24Device.Guid.toString()%3CBR%20%2F%3E%5BPSCustomObject%5D%40%7B%3CBR%20%2F%3EIdentity%20%3D%20%24Device.Identity%20-replace%20%22%5C%5C.%2B%22%3CBR%20%2F%3EDeviceType%20%3D%20%24Device.DeviceType%3CBR%20%2F%3EDeviceOS%20%3D%20%24Device.DeviceOS%3CBR%20%2F%3ELastSuccessSync%20%3D%20%24Stats.LastSuccessSync%3CBR%20%2F%3ELastSyncAttemptTime%20%3D%20%24Stats.LastSyncAttemptTime%3CBR%20%2F%3ELastPolicyUpdateTime%20%3D%20%24Stats.LastPolicyUpdateTime%3CBR%20%2F%3ELastPingHeartbeat%20%3D%20%24Stats.LastPingHeartbeat%3CBR%20%2F%3EClientType%20%3D%20%24Stats.ClientType%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24results%20%2B%3D%20New-Object%20psobject%20-Property%20%24properties%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%24results%20%7C%20Select-Object%20Name%2CUPN%2CFriendlyName%2CDisplayName%2CClientType%2CClientVersion%2CDeviceId%2CDeviceMobileOperator%2CDeviceModel%2CDeviceOS%2CDeviceTelephoneNumber%2CDeviceType%2CFirstSyncTime%2C%20LastSuccessSync%2C%20LastSyncAttemptTime%2C%20LastPolicyUpdateTime%2C%20LastPingHeartbeat%2C%20UserDisplayName%20%7C%20Export-Csv%20-notypeinformation%20-Path%20%24csv%3CBR%20%2F%3E%3CBR%20%2F%3ERemove-PSSession%20%24session%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2892918%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2902291%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20configure%20output%20to%20.csv%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2902291%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F410418%22%20target%3D%22_blank%22%3E%40RichDadufalza%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EYou%20are%20experiencing%20problems%20with%20your%20CSV%20output%20because%20your%20%3CSTRONG%3E%24results%3C%2FSTRONG%3E%20collection%20does%20not%20contain%20all%20of%20the%20properties%20that%20you%20selected.%3C%2FP%3E%3CP%3EIt%20contains%20only%3A%3C%2FP%3E%3COL%3E%3CLI%3EIdentity%3C%2FLI%3E%3CLI%3EDeviceType%3C%2FLI%3E%3CLI%3EDeviceOS%3C%2FLI%3E%3CLI%3ELastSuccessSync%3C%2FLI%3E%3CLI%3ELastSyncAttemptTime%3C%2FLI%3E%3CLI%3ELastPolicyUpdateTime%3C%2FLI%3E%3CLI%3ELastPingHeartbeat%3C%2FLI%3E%3CLI%3EClientType%3C%2FLI%3E%3C%2FOL%3E%3CP%3Eand%20not%3A%3C%2FP%3E%3COL%3E%3CLI%3EName%3C%2FLI%3E%3CLI%3EUPN%3C%2FLI%3E%3CLI%3EFriendlyName%3C%2FLI%3E%3CLI%3EDisplayName%3C%2FLI%3E%3CLI%3EClientVersion%3C%2FLI%3E%3CLI%3EDeviceId%3C%2FLI%3E%3CLI%3EDeviceMobileOperator%3C%2FLI%3E%3CLI%3EDeviceModel%3C%2FLI%3E%3CLI%3EDeviceTelephoneNumber%3C%2FLI%3E%3CLI%3EDeviceType%3C%2FLI%3E%3CLI%3EFirstSyncTime%3C%2FLI%3E%3CLI%3EUserDisplayName%3C%2FLI%3E%3C%2FOL%3E%3CP%3EYour%20resulting%20object%20is%20missing%2012%20properties.%3C%2FP%3E%3CP%3EQuick%20adjustment%20of%20your%20script%20that%20will%20give%20you%20some%20output%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24credentials%20%3D%20Get-Credential%20-Credential%20blank%40blank.onmicrosoft.com%0AWrite-Output%20%22Getting%20the%20Exchange%20Online%20cmdlets%22%0A%0A%24session%20%3D%20New-PSSession%20-ConnectionUri%20https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%20%60%0A-ConfigurationName%20Microsoft.Exchange%20-Credential%20%24credentials%20%60%0A-Authentication%20Basic%20-AllowRedirection%0AImport-PSSession%20%24session%0A%0A%24csv%20%3D%20%22C%3A%5Ctemp%5CMobileDevicesStatistics.csv%22%0A%24results%20%3D%20%40()%0A%24mailboxUsers%20%3D%20get-mailbox%20-resultsize%20unlimited%0A%0A%0Aforeach(%24user%20in%20%24mailboxUsers)%0A%7B%0A%20%20%20%20%24UPN%20%3D%20%24user.UserPrincipalName%0A%20%20%20%20%24displayName%20%3D%20%24user.DisplayName%0A%0A%20%20%20%20%24mobileDeviceList%20%3D%20Get-MobileDevice%20-Mailbox%20%24UPN%0A%0A%20%20%20%20foreach%20(%24Device%20in%20%24MobileDeviceList)%20%7B%0A%20%20%20%20%20%20%20%20%24Stats%20%3D%20Get-MobileDeviceStatistics%20-Identity%20%24Device.Guid.toString()%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%24properties%3D%5Bordered%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Name%3D%24displayName%0A%20%20%20%20%20%20%20%20%20%20%20%20UPN%3D%24UPN%0A%20%20%20%20%20%20%20%20%20%20%20%20Identity%20%3D%20%24Device.Identity%20-replace%20%22%5C%5C.%2B%22%0A%20%20%20%20%20%20%20%20%20%20%20%20DeviceType%20%3D%20%24Device.DeviceType%0A%20%20%20%20%20%20%20%20%20%20%20%20DeviceOS%20%3D%20%24Device.DeviceOS%0A%20%20%20%20%20%20%20%20%20%20%20%20LastSuccessSync%20%3D%20%24Stats.LastSuccessSync%0A%20%20%20%20%20%20%20%20%20%20%20%20LastSyncAttemptTime%20%3D%20%24Stats.LastSyncAttemptTime%0A%20%20%20%20%20%20%20%20%20%20%20%20LastPolicyUpdateTime%20%3D%20%24Stats.LastPolicyUpdateTime%0A%20%20%20%20%20%20%20%20%20%20%20%20LastPingHeartbeat%20%3D%20%24Stats.LastPingHeartbeat%0A%20%20%20%20%20%20%20%20%20%20%20%20ClientType%20%3D%20%24Stats.ClientType%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%24results%20%2B%3D%20New-Object%20psobject%20-Property%20%24properties%0A%0A%20%20%20%20%7D%0A%7D%0A%0A%24results%20%7C%20Export-Csv%20-notypeinformation%20-Path%20%24csv%0A%0ARemove-PSSession%20%24session%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EHope%20that%20helps%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Visitor

Hello all,

I'm new to using Powershell. I found this script online to help display who has a mobile device connected to O365. The script runs fine in PS, but I would like to output the results to .csv and I am not sure how to do that. I tried editing another script I found online, but no success. Any help is greatly appreciated. Here is what I am currently using.

 

$credentials = Get-Credential -Credential blank@blank.onmicrosoft.com
Write-Output "Getting the Exchange Online cmdlets"

$session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-ConfigurationName Microsoft.Exchange -Credential $credentials `
-Authentication Basic -AllowRedirection
Import-PSSession $session

$csv = "C:\temp\MobileDevicesStatistics.csv"
$results = @()
$mailboxUsers = get-mailbox -resultsize unlimited
$mobileDevice = @()

foreach($user in $mailboxUsers)
{
$UPN = $user.UserPrincipalName
$displayName = $user.DisplayName

$mobileDeviceList = Get-MobileDevice -Mailbox $UPN

foreach ($Device in $MobileDeviceList) {
$Stats = Get-MobileDeviceStatistics -Identity $Device.Guid.toString()
[PSCustomObject]@{
Identity = $Device.Identity -replace "\\.+"
DeviceType = $Device.DeviceType
DeviceOS = $Device.DeviceOS
LastSuccessSync = $Stats.LastSuccessSync
LastSyncAttemptTime = $Stats.LastSyncAttemptTime
LastPolicyUpdateTime = $Stats.LastPolicyUpdateTime
LastPingHeartbeat = $Stats.LastPingHeartbeat
ClientType = $Stats.ClientType
}
$results += New-Object psobject -Property $properties
}
}

$results | Select-Object Name,UPN,FriendlyName,DisplayName,ClientType,ClientVersion,DeviceId,DeviceMobileOperator,DeviceModel,DeviceOS,DeviceTelephoneNumber,DeviceType,FirstSyncTime, LastSuccessSync, LastSyncAttemptTime, LastPolicyUpdateTime, LastPingHeartbeat, UserDisplayName | Export-Csv -notypeinformation -Path $csv

Remove-PSSession $session

1 Reply

Hello @RichDadufalza,

You are experiencing problems with your CSV output because your $results collection does not contain all of the properties that you selected.

It contains only:

  1. Identity
  2. DeviceType
  3. DeviceOS
  4. LastSuccessSync
  5. LastSyncAttemptTime
  6. LastPolicyUpdateTime
  7. LastPingHeartbeat
  8. ClientType

and not:

  1. Name
  2. UPN
  3. FriendlyName
  4. DisplayName
  5. ClientVersion
  6. DeviceId
  7. DeviceMobileOperator
  8. DeviceModel
  9. DeviceTelephoneNumber
  10. DeviceType
  11. FirstSyncTime
  12. UserDisplayName

Your resulting object is missing 12 properties.

Quick adjustment of your script that will give you some output:

$credentials = Get-Credential -Credential blank@blank.onmicrosoft.com
Write-Output "Getting the Exchange Online cmdlets"

$session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-ConfigurationName Microsoft.Exchange -Credential $credentials `
-Authentication Basic -AllowRedirection
Import-PSSession $session

$csv = "C:\temp\MobileDevicesStatistics.csv"
$results = @()
$mailboxUsers = get-mailbox -resultsize unlimited


foreach($user in $mailboxUsers)
{
    $UPN = $user.UserPrincipalName
    $displayName = $user.DisplayName

    $mobileDeviceList = Get-MobileDevice -Mailbox $UPN

    foreach ($Device in $MobileDeviceList) {
        $Stats = Get-MobileDeviceStatistics -Identity $Device.Guid.toString()
            
        $properties=[ordered]@{
            Name=$displayName
            UPN=$UPN
            Identity = $Device.Identity -replace "\\.+"
            DeviceType = $Device.DeviceType
            DeviceOS = $Device.DeviceOS
            LastSuccessSync = $Stats.LastSuccessSync
            LastSyncAttemptTime = $Stats.LastSyncAttemptTime
            LastPolicyUpdateTime = $Stats.LastPolicyUpdateTime
            LastPingHeartbeat = $Stats.LastPingHeartbeat
            ClientType = $Stats.ClientType
        }
        $results += New-Object psobject -Property $properties

    }
}

$results | Export-Csv -notypeinformation -Path $csv

Remove-PSSession $session

Hope that helps