#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

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts