Windows 7 Take Ownership only for files+folders of user "karl"?

pstein

Extraordinary Member
Assume I have a huge directory tree with lots of files and folders.
Some of them belong to (simplified) user "karl" (or unknown user "S-1-5-21-107808153") others not.

When I execute now a command like

takeown /f * /a /r /d y

then ALL files+folders get the "Administrators" account as owner.
I don't want this.

I just want to change ownership ONLY for files+folders with owner "karl" (or unknown
user "S-1-5-21-107808153").

How can I achieve this?

All the existing permissions should be kept.

Maybe there is another command which allows an operation "replace user 'karl' with user 'Adminstrators'"?

Peter
 
A little bit of powershell could work, just change $rootPath to the directory you want to iterate through.

Code:
$rootPath = "C:\users"
$targetUsers = @("karl", "S-1-5-21-107808153")

$files = Get-ChildItem -Path $rootPath -Recurse

foreach($file in $files)
{
    $acl = Get-Acl $file
    foreach($targetUser in $targetUsers)
    {
        if($acl.Owner -eq $targetUser)
        {
            TAKEOWN /F "$($file.FullName)" /A /D Y
            break
        }
    }
}
 
Exactly how many files and folders are in your tree? There is a limitation on both of those, I believe it's like 65,000+ folders max. :noway: If you are over that, powershell or CMD commands may not work properly.:headache: You can use your Windows Explorer to easily check that. It may take some time if you are near that upper limit number.
I am a big user of nested folders, and on my 500GB hard drive (small by today's standards), I have 261,553 files in 52,662 folders which is only 43% usage of my drive capacity; well under the 75% max usage limit set by Windows. This only takes 5-10 min. to check, but if your C: drive does exceed the 65,000 folders limit, you may wish to reorganize your C: drive folder setup and do some consolidation and removal, or move some of your folder structure that's not always needed to external backup media to help mitigate the issue.

Best,:encouragement:
<<<BIGBEARJEDI>>> :brew:
 
If there is a limit it's higher that 65k.

Code:
PS C:\WINDOWS\system32> $dirCount = 0
PS C:\WINDOWS\system32> $fileCount = 0
PS C:\WINDOWS\system32> foreach($file in $files) { if($file.Attributes -like "*D*") { $dirCount++ } else {$fileCount++}}
PS C:\WINDOWS\system32> $dirCount
77681
PS C:\WINDOWS\system32> $fileCount
230971
PS C:\WINDOWS\system32>
 
I believe Fat32 had a directory in a directory limit of 63000 ish. NTFS has a file limit of 2^32 - 1 I don't think there is a directory limit beyond the file limit.
 
That sounds familiar to me too. That directory limit is the issue, due to file-path length limits, if the nesting exceed that values you get errors when performing manual backups with File Explorer (Windows Explorer) even in NTFS. I see this all the time on Customer machines who nest 10, 20 levels deep or more without realizing there are limits to the file path length on a filename.:headache: They simply believe it's infinite, which of course it's not; and when doing automatic backups, most folderized backups have a checkbox to turn off or skip copying of files which come up with a filename length error, so those files which there can be hundreds or thousands of never get copied to their backup media).

Also, not too many W7 computers (OP posted under the W7 sub-forum so I'm assuming that's his windows version) were built with FAT32 on their C: bootdrives as you are aware. That practice was abandoned by most of the OEMs anyway in W7. I've seen a very few machines still using FAT32 in Vista, and of course XP. Of course, if this is a self-built PC (we don't have that info from the OP), all bets are off as if he intentionally formatted his C: drive manually with FAT32 for compatibility with older games or something, the directory limit and filename character limit is more of an issue as you said. I've also seen customers who borrow a Windows disc from a friend and reinstall their W7 using this option, or if someone tells them how to do it, they format their C: drive with FAT32 which hasn't been done for years as we know. Also some older accounting programs won't run on a NTFS C: drive, example is one of my Customer's QuickBooks who uses that system for a POS retail purpose. Their old system was XP running Quickbooks v11.0; Intuit dropped support of that version (and the C: drive was formatted as FAT32 in XP) and forced us to upgrade their entire computer to W7 running NTFS on C: running the newer Quickbooks v12.0. We had no choice on this OS upgrade and windows C: drive partition format, as without it we would have lost our ability to perform Credit Card transactions to our Bank on POS purchases. Intuit, as does Peachtree and most other companies today will not support their software on any system using FAT32 formatted C: drive regardless of the windows version in order to run modern versions of their software. [of course, there are always exclusions to this, but not many.].

BBJ
 
A little bit of powershell could work, just change $rootPath to the directory you want to iterate through.

Code:
$rootPath = "C:\users"
$targetUsers = @("karl", "S-1-5-21-107808153")

$files = Get-ChildItem -Path $rootPath -Recurse

foreach($file in $files)
{
    $acl = Get-Acl $file
    foreach($targetUser in $targetUsers)
    {
        if($acl.Owner -eq $targetUser)
        {
            TAKEOWN /F "$($file.FullName)" /A /D Y
            break
        }
    }
}

I like your script. thank you.

Unfortunately I am not an expert in PowerShell.

Assume I want to perform at first a just-show-what-will-be-changed-run.
How can I execute an output on the core line?

It must be something like

Write-Host file to change="$($file.FullName)"

or should I use:

Write-Output file to change="$($file.FullName)"

Can a file/folder have multiple (=more than one) owner?

Whats the difference between the groups "Administrators" and "Administrator"?

Peter

BTW: I have 760000 files in 95000 folders :)
 
The what if version
Code:
$rootPath = "C:\users"
$targetUsers = @("karl", "S-1-5-21-107808153")

$files = Get-ChildItem -Path $rootPath -Recurse

foreach($file in $files)
{
    $acl = Get-Acl $file
    foreach($targetUser in $targetUsers)
    {
        if($acl.Owner -eq $targetUser)
        {
            Write-Host "Changing owner for: $($file.FullName)"
            # TAKEOWN /F "$($file.FullName)" /A /D Y
            break
        }
    }
}

Explanation of the extra $()
$file is a variable, $file.FullName is a property of $file object when adding double quotes everything but variables are interpreted as a string, so the .FullName is treated as a string, by adding $() around it we are creating a temporary variable that contains the data of $file.FullName.

# is used to comment out a line so above the TAKEOWN line will not execute.
 
Back
Top