Home » PowerShell » How to get the File owner using PowerShell

How to get the File owner using PowerShell

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.

In this article, we will discuss how to get the file in windows, and list file owners using PowerShell Get-Acl cmdlet.

Find the owner of the file with PowerShell

To get the file owner using PowerShell, use the Get-Acl cmdlet. Get-Acl cmdlet returns the security descriptor information about a file or resource.

It specifies the permission users or groups have to access files or resources.

Get-Acl D:\LogTest\FTP-02\get-log.py

In the above PowerShell script, we have used the Get-Acl cmdlet and provided the path to the file to get the owner of the file.

The Get-Acl returns the file information and file owner as given below:

PowerShell get file owner
Using PowerShell to get the file owner

Cool Tip: How to search for files in PowerShell!

In the above output, it displays file information like Path, Owner, and Access. However, if you want to get the owner of the file, pipe the results of the Get-Acl cmdlet to the Select-Object cmdlet.

Get-Acl D:\LogTest\FTP-02\get-log.py | Select-Object Owner

Using the Select-Object Owner properties, it will display the file owner as given below.

PS D:\> Get-Acl D:\LogTest\FTP-02\get-log.py | Select-Object Owner                                                      
Owner
-----
ShellGeek\Admin

If you want to check the security descriptor of a file or resource, pipe the Get-Acl cmdlet result to the Format-List cmdlet.

Get-Acl D:\LogTest\FTP-02\get-log.py | Format-List 

The output of the above PowerShell script to display file ownership, path, and Access is as given below:

Path   : Microsoft.PowerShell.Core\FileSystem::D:\LogTest\FTP-02\get-log.py
Owner  : ShellGeek\Admin
Group  : ShellGeek\Domain Users
Access : BUILTIN\Administrators Allow  FullControl
         NT AUTHORITY\SYSTEM Allow  FullControl
         NT AUTHORITY\Authenticated Users Allow  Modify, Synchronize
         BUILTIN\Users Allow  ReadAndExecute, Synchronize
Audit  :
Sddl   : O:S-1-5-21-1417001333-1682526488-839522115-287135G:DUD:(A;ID;FA;;;BA)(A;ID;FA;;;SY)(A;ID;0x1301bf;;;AU)(A;ID;0x1200a9;;;BU)

Cool Tip: How to list files in a directory using PowerShell!

List File Owner using PowerShell

To list file owners for all the files in the specified folders, use the Get-ChildItem cmdlet with Recurse parameter to recursively get items in the folder.

The Get-ChildItem cmdlet result pipes to foreach-object to iterate over the file objects to get the file owner using the Get-Acl cmdlet in PowerShell.

Get-ChildItem D:\LogTest\FTP-02\ -recurse | ForEach-Object{Get-Acl $_.FullName} 

The above PowerShell script lists the file owner for all the files in the specified folder path.

PS D:\> Get-ChildItem D:\LogTest\FTP-02\ -recurse | ForEach-Object{Get-Acl $_.FullName}                                 

    Directory: D:\LogTest\FTP-02


Path                     Owner                                       Access
----                         -----                                            ------
get-log.py           ShellGeek\Admin                    BUILTIN\Administrators Allow  FullControl...
test.txt                ShellGeek\Admin                    BUILTIN\Administrators Allow  FullControl...
srtp.py                ShellGeek\Admin                    BUILTIN\Administrators Allow  FullControl...

Cool Tip: How to get file version in PowerShell!

Conclusion

I hope the above article on using the Get-Acl cmdlet in PowerShell to get the file owner and list file owners is helpful to you.

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

Leave a Comment