Home ยป How to Get File Owner Using PowerShell

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 Contol 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-Item in PowerShell

You can use the Get-Item 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.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.