-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-FileOwner.ps1
More file actions
71 lines (68 loc) · 2.59 KB
/
Get-FileOwner.ps1
File metadata and controls
71 lines (68 loc) · 2.59 KB
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
66
67
68
69
70
71
Function Get-FileOwner {
<#
.SYNOPSIS
Examines the ACL of a file and returns the Owner of that file
.DESCRIPTION
Examines the ACL of a file and returns the Owner of that file. Gives the option of either outputting the results to a file asyncronously or outputting the results syncronously to the screen.
.EXAMPLE
PS C:\> Get-FileOwner -Path 'C:\temp\'
Returns the file owners for the path "c:\temp\"
.EXAMPLE
PS C:\> Get-FileOwner -Path 'C:\temp\' -Recurse
Recursively returns the file owners for the path "c:\temp\"
.EXAMPLE
PS C:\> Get-FileOwner -Path 'C:\temp\' -WriteToFileAsync -OutFilePath 'c:\temp\FileOwnerList.txt' -Recurse
Recursively returns the file owners for the path "c:\temp\" and outputs the results to a file asyncronously.
.PARAMETER Path
Folder path to search for owners. Must be a folder, not a file.
.PARAMETER WriteToFileAsync
Switch to determine if the caller should output the results to a file instead of the screen.
.PARAMETER OutFilePath
File path to output the results.
.PARAMETER Recurse
Specifies whether or not to query ownership info recursively
.Notes
Cmdlet version: 1.0
Written by @charltonstanley
#>
[CmdletBinding(DefaultParameterSetName='WriteToScreen')]
param (
$Path
,
[switch]$Recurse
,
[Parameter(ParameterSetName='WriteToFile')]
[switch]$WriteToFileAsync
,
[Parameter(ParameterSetName='WriteToFile')]
$OutFilePath
)
$Files = Get-ChildItem $path -File
$return =@()
foreach ($file in $Files){
$obj = New-Object system.object
$acl = Get-Acl $File.fullname
$obj|Add-Member -NotePropertyName Path -NotePropertyValue $File.Fullname
$obj|Add-Member -NotePropertyName Owner -NotePropertyValue $acl.Owner
if ($PSCmdlet.ParameterSetName -like 'WriteToFile'){
$obj|Export-Csv -Path $OutFilePath -NoTypeInformation -Append
}
else{
$return += $obj
}
}
if($Recurse){
$directories = Get-ChildItem $path -Directory
if ($PSCmdlet.ParameterSetName -like 'WriteToFile'){
foreach ($directory in $directories){
Get-FileOwner -path $directory.fullname -Recurse -WriteToFileAsync -OutFilePath $OutFilePath
}
}
else{
foreach ($directory in $directories){
$return += Get-FileOwner -path $directory.fullname -Recurse
}
}
}
return $return
}