Public/Backup-specFile.ps1

function Backup-specFile {
    <#
    .SYNOPSIS
    Creates a backup of a specified file.
 
    .DESCRIPTION
    The Backup-specFile function creates a timestamped backup of the specified file in the same directory as the original. The backup file's name is composed of the original file name, followed by the current date and time, and then the original file extension.
 
    .PARAMETER FilePath
    The full path to the file to be backed up. This is a mandatory parameter.
 
    .EXAMPLE
    Backup-specFile -FilePath 'C:\Documents\ImportantFile.txt'
    Creates a backup of 'ImportantFile.txt' in the same directory with a name like 'ImportantFile_20240819-1430.txt'.
 
    .NOTES
    Author: owen.heaume
    Version: 1.0.0 - Initial release
    #>


    param (
        [Parameter(Mandatory = $true)]
        [string]$FilePath
    )

    $fileContent = Get-Content -Path $FilePath

    $directoryPath = Split-Path $FilePath -Parent

    # Backup file contents first
    $date = Get-Date -Format 'yyyyMMdd-HHmm'

    # Use Get-Item to get file info
    $fileInfo = Get-Item $FilePath
    $fileName = $fileInfo.BaseName
    $extension = $fileInfo.Extension

    # Create the backup file name
    $backupFileName = "${fileName}_$date$extension"

    # Create the full backup file path
    $backupFilePath = Join-Path -Path $directoryPath -ChildPath $backupFileName

    # Write backup file contents
    $fileContent | Set-Content -Path $backupFilePath

    Write-Host "Backup created at: $backupFilePath" -ForegroundColor DarkGreen
}