-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Set-ItemOwner.ps1
44 lines (39 loc) · 966 Bytes
/
Set-ItemOwner.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
<#
.SYNOPSIS
Set the ownership of an item - folder or file - to the specified user group
or user.
.PARAMETER Path
The path of the file or directory.
.PARAMETER Group
The name of the user group to allow.
#>
param (
[string] $Path,
[string] $Group = 'administrators'
)
if (!(Test-Elevated (Split-Path -Leaf $PSCommandPath) -warn)) { return }
if ((Get-Item $Path) -is [System.IO.DirectoryInfo])
{
try
{
# /F<name>==path-to-object /R==recurse /A==grant-admins /D:Y=default-prompt-answer
takeown /F $Path /R /A /D Y
# /T==recurse /Q==supress-messages
icacls $Path /grant $Group`:f /t /q
}
catch
{
Write-Host 'Error taking ownership, trying robocopy method...'
$empty = "${env:TEMP}\emptydir"
New-Item $empty -Force -Confirm:$false
robocopy $empty $Path /purge
Remove-Item $empty -Force -Confirm:$false
}
}
else
{
# /F<name>==path-to-object /A==grant-admins
takeown /F $Path /A
# /Q==supress-messages
icacls $Path /grant $Group`:f /q
}