I have the below script retrieving specific file types from the C: drive and outputting specific file properties to a delimited CSV file. I would like to be able to also retrieve the file owner and authors. Any help is very much appreciated.
# PowerShell script to list the .xlsx files under the C Drive
$Dir = get-childitem "C:" -recurse -force
$List = $Dir | where {$_.extension -eq ".xlsx"}
$List |Select-Object fullname, LastAccessTime, LastWriteTime, CreationTime |Export-Csv -path C:\Scripts\xlsx.csv -NoTypeInformation
To get the file owner using PowerShell, pass the file path to the Get-Acl cmdlet in PowerShell. Get-Acl cmdlet returns information about the file which can be used to determine the file owner.
A. The normal method would be to right click on the file in Explorer, select Properties, click the Security tab and click Ownership. This will then show the current owner and give the option to take ownership.
To use Set-Acl , use the Path or InputObject parameter to identify the item whose security descriptor you want to change. Then, use the AclObject or SecurityDescriptor parameters to supply a security descriptor that has the values you want to apply. Set-Acl applies the security descriptor that is supplied.
The best Linux command to find file owner is using “ls -l” command. Open the terminal then type ls -l filename in the prompt. The 3rd column is the file owner. The ls command should be available on any Linux system.
As soon as I posted this I was able to find a solution. I added @{N='Owner';E={$_.GetAccessControl().Owner}}
to the select-object string. It now looks like this:
$List | Select-Object fullname, LastAccessTime, LastWriteTime, CreationTime, @{N='Owner';E={$_.GetAccessControl().Owner}} | Export-Csv -path C:\Scripts\xlsx.csv -NoTypeInformation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With