Checking index fragmentation

In this recipe, we will look at the steps to display index fragmentation using SMO and PowerShell.

Getting ready

We will investigate the index fragmentation of the Person.Person table in the AdventureWorks2014 database.

How to do it...

Let's see the steps required to complete the task:

  1. Open PowerShell ISE as administrator.
  2. Import the SQLPS module and create a new SMO Server object:
    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  3. Add the following script and run:
    $databasename = "AdventureWorks2014" $database = $server.Databases[$databasename] ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.