I am a bit lost in adding my results to a csv.
I tried several way, but the objects are not written to csv.
########################################################## # Var ########################################################## $wdb = Read-Host "Enter Mailbox Database" ########################################################## add-pssnapin microsoft.exchange.management.powershell.e2010 Import-Module ActiveDirectory $ADSettings = Get-adserversettings If ($AdSettings.ViewEntireForest -ne $true) { Set-AdServerSettings -ViewEntireForest $true } ########################################################## function Get-ADUserLastLogon([string]$userName) { $dcs = Get-ADDomainController -Filter {Name -like "*"} $time = 0 foreach($dc in $dcs) { $hostname = $dc.HostName $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon if($user.LastLogon -gt $time) { $time = $user.LastLogon } } $dt = [DateTime]::FromFileTime($time) $AdvStats = Get-mailbox $U.SamAccountName | Get-MailboxStatistics Write-Host "$($username) last logged on at: $($dt), the Mailbox was last accessed on $($AdvStats.LastLogOffTime) by $($AdvStats.LastLoggedOnUserAccount). The mailboxsize is $($Advstats.TotalItemSize) and contains $($AdvStats.ItemCount)." $Item = { @{ $Item.UserName = $userName $Item.LastLogonAD = $dt $Item.LastMailboxLogoff = $AdvStats.LastLogOffTime $Item.LastLoggedOnMailbox = $AdvStats.LastLoggedOnUserAccount $Item.MailboxTotalItemsize = $Advstats.TotalItemSize $Item.MailboxItemCount = $AdvStats.ItemCount } } $Collection = $Item.toString() } ########################################################## $DisAcc = Get-Mailbox -database $wdb -resultsize Unlimited | ? {$_.Identity -like "*/Disabled_Accounts/*"} Foreach ($U in $DisAcc) {Get-AdUserLastLogon $U.SamAccountName} $Collection | Export-Csv -Path C:\Temp\DisabledAcc_$($wdb).csv ########################################################## # End ##########################################################
I also tried with select-Object ... | Export-csv -Path ... -append
Answers provided are coming from personal experience, and come with no warranty of success. I as everybody else do make mistakes.