-
Notifications
You must be signed in to change notification settings - Fork 29
/
Backup-File.ps1
49 lines (38 loc) · 1.04 KB
/
Backup-File.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
<#
.SYNOPSIS
Create a backup as a sibling to a file, with date and time values in the name.
.DESCRIPTION
This provides a quick way to back up a file, usually before making changes that may need
to be reverted.
.INPUTS
System.String, a file path to back up.
.FUNCTIONALITY
Files
.LINK
Copy-Item
.LINK
Split-Path
.LINK
Join-Path
.LINK
Resolve-Path
.LINK
Get-Date
.EXAMPLE
Backup-File.ps1 logfile.log
Copies logfile.log to logfile-201612311159.log (on that date & time).
#>
#Requires -Version 7
[CmdletBinding()][OutputType([void])] Param(
<#
Specifies a path to the items being removed. Wildcards are permitted.
The parameter name ("-Path") is optional.
#>
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ValueFromRemainingArguments=$true)]
[string]$Path
)
Process
{
$name = "$(Split-Path $Path -LeafBase)-$(Get-Date -Format yyyyMMddHHmmss)$(Split-Path $Path -Extension)"
Copy-Item $Path (Resolve-Path $Path |Split-Path |Join-Path -ChildPath $name)
}