I would not recommend automating this process unless you or some other qualified person is going to be around to monitor it and ensure that SQL stops and starts okay. If the script does not work correctly and no one is there to monitor it, you could easily run outside of your outage window and it could not be noticed until you come in to check on it.
Stopping and starting the services should be easy enough, but I do not know of any cmdlet that will run a defrag, so you will have to run the command-line application defrag.exe to acheive that.
You can use the stop-service and start-service cmdlets to stop and start the sql services. I would suggest running two scripts through task scheduler. One to stop the services and run the defrag, and then another towards the end of your maintenance window to check to see if the defrag is still running. If it is, have it stop the defrag and start your sql services.