We help IT Professionals succeed at work.

How do I change the way powershell exports info to a CSV file.

I have successfully created a batch file that runs a powershell script in Exchange Managment Shell to extract the users that have an exchange mobile device partnership to a CSV file. The first column of my CSV file is the "Identity" which looks like this.

Domain.com/ICT/User Accounts/fred flinstone/ExchangeActiveSyncDevices/AirWatchDroid?AirWatch1070560211

It is pulling this information from AD. Is there a way to change or filter out everything from "Identity" except the username?

Here is my script.
Add-PSSnapin Microsoft.Exchange.Management.Powershell.SnapIn -ea silentlyContinue

$UserList = Get-CASMailbox -Filter {hasactivesyncdevicepartnership -eq $true -and -not displayname -like "CAS_{*"} | Get-Mailbox

$UserList | foreach { Get-MobileDeviceStatistics -Mailbox $_} | Sort Identity,DeviceType,DeviceID,FirstSyncTime,LastSuccessSync | select Identity,DeviceType,DeviceID,FirstSyncTime,LastSuccessSync | Export-Csv -Path c:\MobileDeviceCSV\ExchMobileDevice.csv –NoTypeInformation
2015-03-06-092638.jpg
Comment
Watch Question

IT Infrastructure Architect
Commented:
Try to add a custom property..

$UserList | foreach { Get-MobileDeviceStatistics -Mailbox $_} | Sort Identity,DeviceType,DeviceID,FirstSyncTime,LastSuccessSync | select @{N="User";E={($_.Identity -split "/")[-3]}},DeviceType,DeviceID,FirstSyncTime,LastSuccessSync | Export-Csv -Path c:\MobileDeviceCSV\ExchMobileDevice.csv –NoTypeInformation 

Open in new window

chiefsmanSystems Administrator

Author

Commented:
Wow!!!! Dude you are good. What a genius you are! It works exactly how I wanted now. Can you please explain what is going on exactly to display just the user name? I would like to understand how it is working. I am a novice PowerShell nut.

 @{N="User";E={($_.Identity -split "/")[-3]}},

THANKS!!!!
Subash SundharanIT Infrastructure Architect

Commented:
@{N="User";E={($_.Identity -split "/")[-3]}},
Above creates a custom property..
$_.Identity -split "/"
above code splits the string (identity in our case) with "/"
($_.Identity -split "/")[-3]
[-3] selects the third index value from last from the array created by split..
Sample
chiefsmanSystems Administrator

Author

Commented:
Great! Thank you for explaining it.