Get-sqmIndexFragmentation
Performance
sqmSQLTool v1.8.2+ · Diagnostics
sys.dm_db_index_physical_stats (LIMITED mode). Returns fragmentation percentage and automatically recommends:
• REORGANIZE for 5–30% fragmentation
• REBUILD for >30% fragmentation
Results can be filtered by database, table, minimum fragmentation level, and minimum page count.
Execution Flow
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Target SQL Server instance. |
| -SqlCredential | PSCredential | Optional | — | Credentials for the connection. |
| -Database | string | Optional | * = all user DBs | Database name or wildcard pattern (e.g. Sales*). |
| -TableName | string | Optional | * = all tables | Table name or wildcard pattern (e.g. Order*). |
| -MinFragmentationPercent | int | Optional | 5 | Only report indexes with fragmentation ≥ this value. |
| -PageCountMin | int | Optional | 0 | Only report indexes with at least this many pages. |
| -OutputPath | string | Optional | — | Optional CSV export path. |
| -EnableException | switch | Switch | $false | Re-throw exceptions instead of warnings. |
Examples
Check fragmentation in AdventureWorks, minimum 10%
Get-sqmIndexFragmentation -Database 'AdventureWorks' -MinFragmentationPercent 10
Find all fragmented indexes (>30%) on remote instance
Get-sqmIndexFragmentation -SqlInstance 'SQL01' -MinFragmentationPercent 30
Export all fragmentation data to CSV
Get-sqmIndexFragmentation -SqlInstance 'SQL01' -Database 'Sales*' -OutputPath "D:\Reports\fragmentation.csv"