-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAdd-FileAttribute.ps1
65 lines (56 loc) · 2.12 KB
/
Add-FileAttribute.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
65
function Add-FileAttribute {
<#
.SYNOPSIS
Adds a file attribute from specified path (ReadOnly, Hidden, System, Archive)
.DESCRIPTION
Adds a file attribute from specified path (ReadOnly, Hidden, System, Archive)
.PARAMETER Path
A string, or string array, of path to file(s)
.PARAMETER FileAttribute
A string, or string array, that represents file attributes. Valid entries are: (ReadOnly, Hidden, System, Archive)
.EXAMPLE
(Get-Item -Path .\Control.ini).Attributes
Normal
Add-FileAttribute -Path .\Control.ini -FileAttribute Archive, ReadOnly
(Get-Item -Path .\Control.ini).Attributes
ReadOnly, Archive
#>
[cmdletbinding()]
param (
[parameter(Mandatory,HelpMessage='Please specify a file')]
[validatescript({
if (Test-MultipleBool -Bool ($_ | Foreach-Object { Test-Path -Path $_ }) -TestAnd) {
$true
} else {
throw 'ERROR: File(s) specified must exist'
}
})]
[string[]] $Path,
[parameter(Mandatory,HelpMessage='Enter one or more of the following: [ReadOnly, Hidden, System, Archive]')]
[validatescript({
if (Test-MultipleBool -Bool ($_ | Foreach-Object { $_ -in @('ReadOnly', 'Hidden', 'System', 'Archive') }) -TestAnd) {
$true
} else {
throw 'ERROR: The only valid options are: [ReadOnly, Hidden, System, Archive]'
}
})]
[string[]] $FileAttribute
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($File in $Path) {
Write-Verbose "Processing file [$File]"
$File = Get-Item -Path $File -Force
foreach ($Attr in $FileAttribute) {
$Attr = @('ReadOnly', 'Hidden', 'System', 'Archive') | where-object { $_ -eq $Attr }
Write-Verbose " adding attribute [$Attr]"
$File.Attributes = $File.Attributes -bor [System.IO.FileAttributes]::$Attr
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}