6

#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=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $env:ComputerName
$database = $server.databases["MyDB"]
$database.AutoShrink = $true
$database.Alter()
Filed in: Columns, Tips and Tricks Tags: , , ,

6 Responses to "#PSTip Changing SQL database AutoShrink property using SMO and PowerShell"

  1. Just because you can doesn’t mean you should. Please see Argenis’ comment 🙂

    • Ravikanth says:

      Absolutely! We never meant you should go ahead and change. If you want to, there is a way to do that in PowerShell and SMO. That is all the tip is about! 🙂

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: