Tag Archives: wbadmin

Windows Server Backup Powershell Script

This is a small Powershell script that will use the command line version of Windows Server Backup (wbadmin). It has the ability to email you upon failure or success. It will also include the log as an attachment to the script.

The variables that needs to be edited should be obvious.

I’ve found parts of this script while googling, but I’ve added a small logic that will create the backup location if it does not exist already.

#—————————————–Start script—————————————————-

function SendEmail($To, $From, $Subject, $Body, $attachment, $smtpServer) 
        Send-MailMessage -To $To -Subject $Subject -From $From -Body $Body -Attachment $attachment -SmtpServer $smtpServer 
$day=(get-date -f dd-MM-yyyy)
$backuplog="$backuplocation"+(get-date -f dd-MM-yyyy)+"-backup-$hname.log" 

function Out-FileForce {
    if(Test-Path $backuplocation)
        Out-File -inputObject $_ -append -filepath $backuplocation
        new-item -force -path $backuplocation -value $_ -type file

Write-Output ("———————– Backup started on – $(Get-Date –f o) ————————-") | Out-FileForce "$backuplog" 
wbadmin start backup -backupTarget:$backuplocation -include:c: -systemstate -allcritical -vsscopy -quiet | Out-FileForce "$backuplog" 
        Write-Output ("———————– An error has occurred! Check it please!. – $(Get-Date –f o) ————————-") | Out-File "$backuplog" -Append 
        SendEmail -To "$emailto" -From "$emailfrom" -Subject "backup failed" -Body "The backup has failed! Please check attached log." -attachment "$backuplog" -smtpServer "$emailserver" 

Write-Output ("———————– Everything is OK! – $(Get-Date –f o) ————————-") | Out-File "$backuplog" -Append 
SendEmail -To "$emailto" -From "$emailfrom" -Subject "backup $hname ok" -Body "The backup has succeeded!" -attachment "$backuplog" -smtpServer "$emailserver" .

#——————————————–End script——————————————————

// CrashMAG