#PSTip Changing SQL database AutoShrink property using SMO and PowerShell

Note: This tip requires PowerShell 2.0 or above.

In today’s tip, we will see how we can use SQL SMO and PowerShell to change the AutoShrink property of a SQL database. This property specifies whether the size of the database is automatically reduced when a large amount of available space occurs. The AutoShrink property takes a Boolean value. So, setting it to $true will enable auto-shrink of the database and $false will disable it.

Let us see how we can change this.

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $env:ComputerName
$database = $server.databases["MyDB"]
$database.AutoShrink = $true
Share on: