0

I wrote a POWERSHELL-script for displaying a distribution group based on lastlogontime but i was able to to export it into CSV-format but gives wrong results.

My code is as follows:

$users=Get-DistributionGroupMember  -Identity "ALL Group" | select -Expand SamAccountName   
$data=foreach($userss in $users){
Get-MailboxStatistics $userss |   sort LastLogonTime -Descending 
}
$data | Export-Csv "$home\Desktop\Folders.csv" -NoTypeInformation
kira.tk
  • 13
  • 1
  • 5
  • 1
    Please _edit the question_ and explain what's wrong. No results in CSV? Wrong results? Something else? – vonPryz Dec 31 '19 at 13:54
  • Are you on on-premises Exchange 2016? In that case, the parameter you are sending as user may be interpreted as `-StoreMailboxIdentity` instead of `-Identity`. According to [the docs](https://learn.microsoft.com/en-us/powershell/module/exchange/mailboxes/get-mailboxstatistics?view=exchange-ps) both these parameters use the same position.. Try naming the parameter for the user with `Get-MailboxStatistics -Identity $userss` – Theo Jan 01 '20 at 12:20

1 Answers1

0

You can use this:

Get-DistributionGroupMember  -Identity "ALL Group" | Select-Object -ExpandProperty SamAccountName | foreach { Get-MailboxStatistics $_ | sort-object LastLogonTime -Descending }  | Export-csv "CSVpath"

There is no such switch of select-Object named expand.

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • FYI Parameters of a given cmdlet only need to have enough of it to make it distinct within that cmdlet. In this case `-Expand` is more than enough to have PowerShell know `-ExpandProperty` is what is meant here. – Theo Jan 01 '20 at 12:24