The script is available for download from http://gallery.technet.microsoft.com/How-to-rebuild-or-12b01775. You can also use Microsoft Script Browser for Windows PowerShell ISE to download the sample with one button click from within your scripting environment.
This PowerShell 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 to rebuild or reorganize the indexes according to their defragmentation. If avg_fragmentation_in_percent_value is higher than 30 and page_cout is more than 1000, we can rebuild the indexes. If avg_fragmentation_in_percent_value is between 5 and 30 and page_count is more than 1000, we can reorganize the indexes.
You can find more All-In-One Script Framework script samples at https://aka.ms/onescriptingallery