The script is available for download in Microsoft Script Browser for Windows PowerShell ISE. You need to install the Script Browser application first, and search for the script sample title.
This T-SQL script sample shows how to rebuild or reorganize indexes in a specified database.
This script will list all the indexes in a database, we can choose rebuild or reorganize the indexes according to their defragmentation. If avg_fragmentation_in_percent_value is higher than 30, we can rebuild the indexes. If avg_fragmentation_in_percent_value is between 5 and 30, we can reorganize the indexes.
You can find more All-In-One Script Framework script samples at https://aka.ms/onescriptingallery