Defrag Scheduled task on Win 8 / Win 12:

http://blogs.technet.com/b/askcore/archive/2014/02/17/what-39-s-new-in-defrag-for-windows-server-2012-2012r2.aspx

In brief, you need to keep these things in mind:

The default scheduled task for defrag is as follows:

– Windows Server 2008R2: defrag.exe –c

– Windows Server 2012: defrag.exe –c –h –k

– Windows Server 2012 R2: defrag.exe –c –h –k –g

On a client machine it will be Windows –c –h –o     however, if there is a thin provisioned media present, defrag will do slab consolidation as well.

The command line –c –h –k (for 2012) and –c –h –k –g (for 2012R2) for the defrag task will perform storage optimization and slab consolidation on thin provisioned media as well. Different virtualization platforms may report things differently, like Hyper-V shows the Media Type as Thin Provisioned, but VMware shows it as a Hard disk drive. The fragmentation percentage shown in the defrag UI has nothing to do with slab consolidation. It refers to the file fragmentation of the volume.  If you want to address file fragmentation you must run defrag with –d(already mentioned before)

If you are planning to deploy a PowerShell script to achieve the same, the command is simple.

PS C:\> Optimize-Volume -DriveLetter <drive letter name> -Defrag -Verbose

http://www.petri.co.il/manage-scheduled-tasks-windows-8-windows-server-2012-powershell-part-1.htm#

You can use a Sysinternal tool Contig.exe to check the fragmentation level before deciding to defrag. The tool is available here.

Free tool (sourceforge): UltraDefrag: http://ultradefrag.sourceforge.net/en/index.html

Paying software (condusiv): http://www.condusiv.com