Invoke-sqmLogShrink
PerformancesqmSQLTool v1.8.2+ · Maintenance✓ -WhatIf supported
DBCC SHRINKFILE. Target size is calculated as a percentage of current size (ShrinkTargetPercent, default 10%) with a minimum threshold (MinTargetMB, default 64 MB). For Always On AG members, automatically redirects to the primary replica. System databases and offline databases are skipped.Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Target SQL Server instance. Auto-redirects to AG primary if applicable. |
| -SqlCredential | PSCredential | Optional | — | Credentials for the connection. |
| -Database | string[] | Optional | All user DBs | Target databases (wildcards allowed). System and tempdb always skipped. |
| -All | switch | Optional | $false | Explicitly process all user databases. |
| -ShrinkTargetPercent | int | Optional | 10 | Target as % of current log size (1-99). |
| -MinTargetMB | int | Optional | 64 | Minimum target size in MB (floor limit). |
| -ContinueOnError | switch | Switch | $false | Continue to next database on error. |
| -EnableException | switch | Switch | $false | Re-throw exceptions immediately. |
| -WhatIf / -Confirm | switch | Optional | — | ShouldProcess (ConfirmImpact: None) guards shrink execution. |
Execution Flow
Examples
Shrink log file to 20% of current size
Invoke-sqmLogShrink -Database "MyDB" -ShrinkTargetPercent 20
All databases with -WhatIf
Invoke-sqmLogShrink -SqlInstance "SQL01" -All -WhatIf