Public/Remove-VMBackup.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function Remove-VMBackup {
    <#
    .SYNOPSIS
    Remove old backup files.

    .DESCRIPTION
    Choose how long you want to keep a backup of your VMs. This cmdlet looks
    at the age of the files in the folder specefied and deletes them if they are
    older than the set in the Retention parameter.

    .PARAMETER Destination
    Specify the location of the stored backups.

    .PARAMETER Retention
    Configure how long you want to keep the backups. Default is 30 days.

    .PARAMETER Force
    The Force parameter allows the user to skip the "Should Continue" box.

    .EXAMPLE
    Remove-VMBackup -Destination 'D:\Backup\Hyper-V' -Retention 21

    Removes any files older than 21 days in the directory D:\Backup\Hyper-V

    .NOTES
    Author: Frederik Hjorslev Poulsen

    .LINK
    https://hjorslev.github.io/HyperVBackup/Remove-VMBackup.html
    #>


    [CmdletBinding(SupportsShouldProcess = $true,
        ConfirmImpact = 'Medium')]
    param (

        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [Alias('BackupDestination')]
        [string]$Destination,

        [Parameter(Mandatory = $false)]
        [int]$Retention = 30,

        [Parameter(Mandatory = $false)]
        [switch]$Force
    )

    process {
        $OldBackups = Get-ChildItem -Path $Destination -Recurse -Force | Where-Object -FilterScript { (((Get-Date)-$_.LastWriteTime) -gt (New-TimeSpan -Day $Retention)) }
        $RemoveOldSCMessage = @"
Do you want to remove any backups or files stored in $($Destination) older than $($Retention) days?
"@

        if ($null -ne $OldBackups) {
            if ($Force -or $PSCmdlet.ShouldContinue($RemoveOldSCMessage, 'Remove old backups?')) {
                foreach ($OldBackup in $OldBackups) {
                    Write-Verbose -Message "Deleteing $($OldBackup) because it is older than $($Retention)."
                }

                $OldBackups | Remove-Item -Recurse -Force
            }
        }
    } # Process
} # Cmdlet