4

I am new to Powershell and have been using the following snippet of code to pull the site quota information from my farm

$GetStorage = @{Name="Storage"; Expression={"{0:N2} MB" -f ($_.Usage.Storage/1048576)}}
$GetQuota = @{Name="Quota"; Expression={"{0:N2} MB" -f ($_.Quota.StorageMaximumLevel/1048576)}}
$GetUsage = @{Name="Used"; Expression={"{0:P2}" -f ($_.Usage.Storage/$_.Quota.StorageMaximumLevel)}}
Get-SPSite | Select-object URL,$GetStorage, $GetQuota, $GetUSage | Sort-Object Storage | Format-Table -AutoSize

This produces output like this:

Url                                 Storage     Quota        Used   
---                                 -------     -----        ----   
https://intdomain.com/sites/digital 156.93 MB   5,120.00 MB  3.07 % 
https://intdomain.com/sites/brazil  340.42 MB   5,120.00 MB  6.65 % 
https://intdomain.com/sites/pos     51.90 MB    2,560.00 MB  2.03 % 
https://intdomain.com/sites/EMEA    549.05 MB   2,560.00 MB  21.45 %
https://intdomain.com/sites/APAC    6,918.31 MB 10,240.00 MB 67.56 %
https://intdomain.com/sites/pr      655.82 MB   2,560.00 MB  25.62 %
https://intdomain.com               74.04 MB    2,560.00 MB  2.89 % 

As you'll see from my code, I'm trying to sort by the storage consumed, and it's only sorting it via the first number displayed - hence I have 6GB then 600MB

I'd like to be able to sort by this the quota used (the % column) but I have the same issue. The numbers do not sort in the right way.

I believe that it's because the numbers being returned are "Hash Tables" and not Integers.

Could someone help me correct the output so that it's sorted correctly?

2 Answers 2

1

Yes, it has to do with the number formatting. You've made Storage, Quota, and Used all strings. The results you see are consistent with sorting strings of numerical text.

Instead of having the units as part of the results, just use the numbers and then format the results in the table with a formatting expression:

$GetStorage = @{Name="Storage"; Expression={$_.Usage.Storage/1048576}}
$FormatStorage = @{label="Storage";Expression={$_.Storage}; FormatString="{0:N2} MB"}
$GetQuota = @{Name="Quota"; Expression={$_.Quota.StorageMaximumLevel/1048576}}
$FormatQuota = @{label="Quota";Expression={$_.Quota}; FormatString="{0:N2} MB"}
$GetUsage = @{Name="Used"; Expression={$_.Usage.Storage/$_.Quota.StorageMaximumLevel}}
$FormatUsage = @{label="Used";Expression={$_.Used}; FormatString="{0:P2}"}
Get-SPSite -Limit All | Select-object URL,$GetStorage, $GetQuota, $GetUSage | Sort-Object Storage | Format-Table URL, $FormatStorage, $FormatQuota, $FormatUsage -AutoSize

I also added a -Limit All parameter to the Get-SPSite so you'll get all the site collections in the farm.

2
  • I tried your code above, but no matter what column I sort by, it's still out of sequence.
    – Rob Sked
    Feb 11, 2015 at 15:31
  • 1
    Yes, you are correct. I was still sorting on a string. I've updated the code to use a separate expression for formatting that is used when building the table after the sort.
    – shufler
    Feb 11, 2015 at 18:14
0

This is the Powershell script that I wrote to retrieve quota and size information for Site Collections in SharePoint. Run it as a SharePoint administrator to ensure that you can retrieve all the data. $spQuotaInfo is properly sortable, filterable, etc. and works with Export-Csv.

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

function Get-SPQuotaTemplate($spSite)
{
    return $spSite.WebApplication.WebService.QuotaTemplates | ?{ $_.QuotaID -eq $spSite.Quota.QuotaID }
}

function Get-SPApplicableQuota($spSite)
{
    [Microsoft.SharePoint.Administration.SPQuota] $spSiteQuota = $spSite.Quota

    if ($spSite.Quota.QuotaID -ne 0)
    {
        $spSiteQuotaTemplate = Get-SPQuotaTemplate $spSite

        if ($spSiteQuotaTemplate -ne $null)
        {
            $spSiteQuota = [Microsoft.SharePoint.Administration.SPQuota] $spSiteQuotaTemplate
        }
    }

    return $spSiteQuota
}

function Get-SPQuotaInfo()
{
    $quotaIDColumn = @{name = 'QuotaID'; expression = { $_.Quota.QuotaID } }
    $quotaTemplateColumn = @{name = 'QuotaTemplate'; expression = { $quotaID = $_.Quota.QuotaID; if ($quotaID -eq 0) { "No Template" } else { ((Get-SPQuotaTemplate $_).Name, "Unknown ($($quotaID))" -ne $null)[0] } } }
    $storageMaximumLevelColumn = @{ name = 'StorageMaximumLevel'; expression = { (Get-SPApplicableQuota $_).StorageMaximumLevel } }
    $storageWarningLevelColumn = @{ name = 'StorageWarningLevel'; expression = { (Get-SPApplicableQuota $_).StorageWarningLevel } }
    $storageUsedColumn = @{ name = 'StorageUsed'; expression = { $_.Usage.Storage } }

    $spQuotaInfo = $null

    # Sites must be retrieved with elevated privileges in order to ensure consistent access to the usage data.
    [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
        [Microsoft.SharePoint.SPSite[]] $spSites = Get-SPSite -Limit ALL
        $spQuotaInfo = $spSites | Select Url, $quotaIDColumn, $quotaTemplateColumn, $storageMaximumLevelColumn, $StorageWarningLevelColumn, $storageUsedColumn

        # Passes the array of PSCustomObjects generated by the Select back out to the parent.
        Set-Variable -Scope 1 -Name spQuotaInfo -Value $spQuotaInfo
    })

    return $spQuotaInfo
}

function Format-QuotaInfoTable($spQuotaInfo)
{
    $quotaTemplateDisplay = @{name = 'Quota Template'; expression = { $_.QuotaTemplate } }
    $storageMaximumLevelDisplay = @{ name = 'Storage Maximum Level'; expression = { if ($_.StorageMaximumLevel -eq 0) { "No Limit" } else { "{0:N0} MB" -f $_.StorageMaximumLevel / 1048576.0 } }; align = 'right' }
    $storageWarningLevelDisplay = @{ name = 'Storage Warning Level'; expression = { if ($_.StorageWarningLevel -eq 0) { "No Limit" } else { "{0:N0} MB" -f ($_.StorageWarningLevel / 1048576.0) } }; align = 'right' }
    $storageUsedDisplay = @{ name = 'Storage Used'; expression = { ($_.StorageUsed / 1048576.0) }; align = 'right'; formatString = '{0:N3} MB' }
    $percentQuotaUsedDisplay = @{ name = 'Quota Used'; expression = { if ($_.StorageMaximumLevel -eq 0) { 0 } else { $_.StorageUsed / $_.StorageMaximumLevel } }; formatString = '{0:P}' }

    $spQuotaInfo | ft -AutoSize Url, $quotaTemplateDisplay, $storageMaximumLevelDisplay, $storageWarningLevelDisplay, $storageUsedDisplay, $percentQuotaUsedDisplay
}

$spQuotaInfo = Get-SPQuotaInfo | Sort StorageUsed -Descending
Format-QuotaInfoTable ($spQuotaInfo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.