SharePoint administrators often need to run scheduled PowerShell against SharePoint. One particular need for this is when backing up site collections.
1. Create a PowerShell script
Tip: when working with notepad on the server, open it running it ad administrator. This will make sure that the files you save have the relevant authority.
Save the below commands in a text file (updating them with your details) and then do a save as and save it with the .ps1 extension.
Add-PSSnapin Microsoft.SharePoint.PowerShell
Backup-spsite -identity https://mydomain/sites/brightwork -path C:BackupBackup.bak –force;
Note: -force overwrites the existing file if it exists already at that path.
Note: When running backup commands or other SharePoint commands manually you may be used to running these in the SharePoint Management Shell. If you save these commands as a PowerShell file (.ps1) and want to schedule it to run (from windows task scheduler), the file by default will be run under Windows PowerShell and not the SharePoint Management Shell. Therefore you must call the SharePoint Snapin by adding Add-PSSnapin Microsoft.SharePoint.PowerShell to your ps1 file.
Add Email Notification
If you would like to receive an email each time this .ps1 script runs why not add the following to the script your created above.
$emailFrom = “user@yourdomain.com“
$emailTo = “user@yourdomain.com“
$subject = “your subject”
$body = “your body”
$smtpServer = “your smtp server ip”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
2. Create a Batch File to run the PowerShell
Save the below commands in a text file (updating them with your details) and then do a save as and save it with the .bat extension.
powershell -command C:ScriptsBackupSPSite.ps1
3. Schedule Windows Task Scheduler to Run the Batch File
See “To create a task by using the Windows interface” from Microsoft.