list mailbox from biggest to smalles and put out in a csv file

Hi People,

I have a script to get al usermailboxes in exchange online.

But i tried serveral options to list everything in descending and tried to get the output in a csv file. but failed

any one the solution for me ?

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $admin -Authentication Basic -AllowRedirection
$ImportCmd = Import-PSSession $Session

$userToFind = Read-Host -Prompt "Enter user to find (leave blank for all)"

$params = @{}
if([string]::IsNullOrEmpty($userToFind) -eq $false)
{$params = @{Identity = $userToFind}
}



$UserMailboxStats = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited @Params | Get-MailboxStatistics
$UserMailboxStats | Add-Member -MemberType ScriptProperty -Name TotalItemSizeInBytes -Value {$this.TotalItemSize -replace "(.*\()|,| [a-z]*\)", ""}
$UserMailboxStats | Select-Object DisplayName, TotalItemSizeInBytes,@{Name="TotalItemSize (GB)"; Expression={[math]::Round($_.TotalItemSizeInBytes/1GB,2)}}

Should be this simple:

#Sort the stats on Bytes
$sortedUserMailboxStats = $UserMailboxStats | Sort-Object -Property TotalItemSizeInBytes -Descending
#Export to CSV
$sortedUserMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation

Yeah great,

I tested it and it looked it worked but i got the following error message

Export-CSV : Toegang tot het pad C:\UserMailboxStats.csv is geweigerd.
At Q:\Scripts\Test\list mailboxs Exchange online.ps1:23 char:27
+ ... rMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

and it is not listing from bigget to smalles

My script so far, (i’m very new with this, if someone has a tip to learn for scripting let me know)

$Username = "username"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$Admin = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $admin -Authentication Basic -AllowRedirection
$ImportCmd = Import-PSSession $Session

$userToFind = Read-Host -Prompt "Enter user to find (leave blank for all)"

$params = @{}
if([string]::IsNullOrEmpty($userToFind) -eq $false)
{$params = @{Identity = $userToFind}
}


$UserMailboxStats = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited @Params | Get-MailboxStatistics
$UserMailboxStats | Add-Member -MemberType ScriptProperty -Name TotalItemSizeInBytes -Value {$this.TotalItemSize -replace "(.*\()|,| [a-z]*\)", ""}
$UserMailboxStats | Select-Object DisplayName, TotalItemSizeInBytes,@{Name="TotalItemSize (GB)"; Expression={[math]::Round($_.TotalItemSizeInBytes/1GB,2)}}

#Sort the stats on Bytes
$sortedUserMailboxStats = $UserMailboxStats | Sort-Object -Property TotalItemSizeInBytes -Descending
#Export to CSV
$sortedUserMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation

You just need to provide a CSV path that you have access to write to, you are getting access denied creating the CSV. Just change the bolded path to your desktop or somewhere you have permissions to write\create:

$sortedUserMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation