duaneleem
7/30/2016 - 5:39 PM

This will make a VHDX backup of the drives you specify to an external drive (such as I drive) using Disk2vhd.

This will make a VHDX backup of the drives you specify to an external drive (such as I drive) using Disk2vhd.

: This batch file uses Disk2vhd to make automated backups of the entire Windows computer.
:: Download Disk2vhd from: 
:: https://technet.microsoft.com/en-us/sysinternals/ee656415.aspx?f=255&MSPPError=-2147217396

:: Generate the file name.
$fileName = (get-date -uformat %d%b%y) + "_" `
    + $Env:COMPUTERNAME + ".vhdx"

:: Drive letter to save the VHDX.
$driveLetter = "i:"
$saveFolder = "disk2vhd"

:: Process backup creation using disk2vhd.
c:\disk2vhd c: d: e: g: h: ($driveLetter + "\" + $saveFolder + "\" + $fileName) -accepteula | wait-job

:: Check if path exists and if not, exit script.
IF NOT EXIST ($driveLetter + "\" + $saveFolder) EXIT

:: CD into the drive letter.
cd $driveLetter

:: Remove EVERYTHING except the new file that was just created.
remove-item ($driveLetter + "\" + $saveFolder)\* -exclude "$fileName", admin* -force