WindowsBackupAdmin.psm1

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# Resolve the packaged scripts that contain the tested implementation.
$script:ScriptsRoot = Join-Path $PSScriptRoot 'Scripts'
$script:ViewScript   = Join-Path $script:ScriptsRoot 'View-WindowsBackup.ps1'
$script:DeleteScript = Join-Path $script:ScriptsRoot 'Delete-WindowsBackup.ps1'

function Get-WindowsBackup {
    <#
    .SYNOPSIS
        View and optionally export a user's Windows settings backup data.
    .DESCRIPTION
        Views your own backup, or another user's backup when you hold the
        Microsoft 365 Backup Administrator role. Optionally exports the data.
 
        Alias: View-WindowsBackup
    .PARAMETER UserId
        UPN or object id of the user. Omit to view your own backup.
    .PARAMETER ExportPath
        Folder where a JSON/ZIP export is saved if you choose to export.
    .PARAMETER TopDevices
        How many devices to list before summarizing the rest.
    .PARAMETER Fresh
        Force a clean sign-in to switch accounts.
    .EXAMPLE
        Get-WindowsBackup
    .EXAMPLE
        Get-WindowsBackup -UserId user@contoso.com
    #>

    [CmdletBinding()]
    param(
        [string] $UserId,
        [string] $ExportPath = $env:USERPROFILE,
        [int]    $TopDevices = 15,
        [switch] $Fresh
    )
    & $script:ViewScript @PSBoundParameters
}

function Remove-WindowsBackup {
    <#
    .SYNOPSIS
        Permanently delete ALL Windows settings backup data for a user.
    .DESCRIPTION
        Irreversibly deletes all backup data for the specified user. The script
        shows what will be removed and prompts for confirmation. Requires the
        Microsoft 365 Backup Administrator role.
 
        Alias: Delete-WindowsBackup
    .PARAMETER UserId
        UPN or object id of the user whose backup to delete.
    .PARAMETER Fresh
        Force a clean sign-in to switch accounts.
    .EXAMPLE
        Remove-WindowsBackup -UserId user@contoso.com
    #>

    [CmdletBinding()]
    param(
        [string] $UserId,
        [switch] $Fresh
    )
    & $script:DeleteScript @PSBoundParameters
}

# Backward-compatible aliases so administrators can keep using the familiar names.
Set-Alias -Name 'View-WindowsBackup'   -Value 'Get-WindowsBackup'
Set-Alias -Name 'Delete-WindowsBackup' -Value 'Remove-WindowsBackup'

Export-ModuleMember -Function 'Get-WindowsBackup', 'Remove-WindowsBackup' `
                    -Alias 'View-WindowsBackup', 'Delete-WindowsBackup'