How to Get File Owner Using PowerShell
To get file owner using PowerShell, you can use the Get-Acl or Get-Item cmdlets. These commands retrieve the Access Control List (ACL) of a file or directory and then access the Owner property.
The following method shows how you can do it.
Method 1: Get the file owner using Get-Acl
# specify the path of a file
$filePath = "C:\temp\log\system_log.txt"
# Get the ACL of the file
$fileAcl = Get-Acl -Path $filePath
# Get the file owner of the file
$fileAcl.Owner
This example will display the file owner.
Method 2: Get the file owner using Get-Item
# specify the path of a file
$filePath = "C:\temp\log\system_log.txt"
# Get the file owner
(Get-Item -Path $filePath).GetAccessControl().Owner
This example will return the owner of a file.
The following examples show how to use these methods in practice.
Get File Owner Using Get-Acl in PowerShell
You can use the Get-Acl cmdlet in PowerShell to retrieve the Access Control List (ACL) for a file and then access the Owner property to get the owner of a file.
# specify the path of a file
$filePath = "C:\temp\log\system_log.txt"
# Get the ACL of the file
$fileAcl = Get-Acl -Path $filePath
# Get the file owner of the file
$fileAcl.Owner
Output:
ADT\Admin
In this PowerShell script, the $filePath variable holds the file path. The Get-Acl command uses the -Path variable to specify the $filePath and get the Access Control List (ACL) for a file.
Finally, the Owner property of ACL for a file returns the owner of a file.
How to Get File Owner Using Get-Item in PowerShell
Another way to get file owner in PowerShell is by using the Get-Item cmdlet. This command uses the GetAccessControl() method to get the Access Control List (ACL) and finally accesses its Owner property to get the file owner name.
# specify the path of a file
$filePath = "C:\temp\log\system_log.txt"
# Get the file owner
(Get-Item -Path $filePath).GetAccessControl().Owner
Output:
ADT\Admin
This PowerShell script returns the ownership of a file.
Conclusion
I hope the above article on getting file ownership using PowerShell is helpful to you.
Related Articles
File Information & Properties
- PowerShell Get File Properties - File metadata
- PowerShell Get File Extension - Extract extension
- PowerShell Get File Hash - File integrity checking
- PowerShell Get File Version - Version information
- PowerShell Get MD5 Hash - Hash calculation
File Management Operations
- PowerShell List Files - List directory contents
- PowerShell Directory Listing - Navigate directories
- PowerShell Delete All Files - Delete files
- PowerShell Rename Files - Rename files in bulk
Access Control & Security
- PowerShell Check If Registry Key Exists - Registry ACL
- PowerShell Set Registry Value - Set security values
Data Selection & Filtering
- PowerShell Select-Object - Select file properties
- PowerShell Where-Object - Filter files by criteria
- PowerShell ForEach-Object - Process each file
- PowerShell Get-ChildItem Filter - Advanced filtering
Display & Output
- PowerShell Format Table - Format output
- PowerShell Write-Output - Display output
- PowerShell Output to File - Write to files
Control Flow & Logic
- PowerShell If-Else Statement - Conditional logic
- PowerShell For Loops - Loop through files
- PowerShell Try-Catch - Error handling
Variables & Collections
- PowerShell Variables - Store file data
- PowerShell Arrays - Work with file arrays
- PowerShell Hashtables - Store mappings
- PowerShell Add-Member - Add custom properties
Output & Export
- PowerShell Export CSV - Export to CSV
- PowerShell Import CSV - Import from CSV
File Path Operations
- PowerShell Test-Path - Check path existence
- PowerShell Get-Item - Get item objects
Functions & Automation
- PowerShell Functions - Create reusable functions
- PowerShell Measure-Object - Calculate statistics
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell reference
- Complete PowerShell Tutorial - Comprehensive course