Powershell query for all files without an owner?

Tonito Dux 976 Reputation points
2023-04-27T09:47:32.7333333+00:00

Hi,

we are migrating a 2012R2 file server to a new 2022 file server and during the migration process with the storage migration service we got 23 000 files that receive an error 5 which is access denied error. Upon examination of some of the files they all got the same problem - no owner and no ACLs.

Would like a PS query/script which will go trough a folder and sub-folders and report back all of the files without the owner set.

Thank you for your time and assistance!

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. MotoX80 36,291 Reputation points
    2023-04-27T22:56:48.5366667+00:00

    Here's a script that might help you. Test it out on a small number of folders and verify that resetting the inheritance is what you want it to do.

    If you have multiple levels of folders that you do not have access to, then you will need to run this script multiple times to get to all of the files/folders.

    cls
    $noaccess = @()
    $all = Get-ChildItem c:\temp\zzzz -recurse -Force   -ErrorAction SilentlyContinue       # set folder path here
    
    foreach ($f in $all){                     
        $f.fullname                     # show what we are processing
        try {
            $a = Get-Acl $f.fullname -ErrorAction Stop
            if ($a.access.Count -eq 0) {                  # no acls!!!! 
                $noaccess += $f.fullname                # add to the fix list
                "No acls, adding to noaccess list."
                }            
        } catch {
            "Unable to access, adding to noaccess list."
            $noaccess += $f.fullname 
        }    
    }
    ""
    "Here are the files and folders we could not access."
    $noaccess
    
    if ((Read-Host "Enter y to fix") -ne 'y') {return}
    
    # Now fix the problem, you must be running this script with UAC admin access 
    foreach ($f in $noaccess) {
        "takeown.exe /f $f"
        takeown.exe /f "$f"
        "icacls.exe $f /reset"
        icacls.exe "$f" /reset
    }
    

  3. Rich Matheisen 47,901 Reputation points
    2023-04-28T01:50:43.0933333+00:00

    This won't fix the problem but it should run faster than using try/catch and Get-ACL. You can use the CSV it creates and the last part of the code from @MotoX80 to add the owner.

    $folderlist = "c:\junk","c:\T-Copy"    # directories whose files should be examined
    $folderlist |
        ForEach-Object{
            Get-ChildItem "$_\*" -file -Recurse -Depth 100 |    # adjust the depth value as appropriate
                ForEach-Object{
                    if ( $null -eq $_.GetAccessControl().Owner){    # if there's no ACL this should report that file too
                        Select-Object fullname, LastAccessTime, LastWriteTime, CreationTime
                    }
                }
        } | Export-Csv c:\junk\OwnerlessFiles.csv -NoTypeInformation
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.