Can someone suggest a command line utility to retrieve Windows file ownership?

Posted on 2011-10-14
Last Modified: 2012-06-27
The DIR command can return file ownership when one uses the /q argument. But it seems to truncate long user names.

While "dir /q myfile.txt" successfully returns the owner with a short name:
10/13/2011  01:38 PM           355,398 CITNET\Ritu Mishra    myfile.txt

It does not work for a name like Lenny Van Duyvendijk
10/05/2011  12:59 PM               162 CITNET\Lenny Van DuyvenCONTROL_TOTALS_BANK_20110930.TXT

I have not been able to find any combination of DIR command switches to successfully return such a long (and multi-part) last name.

I am looking for a command line/non-GUI utility, since this information will be pulled into another program (Microsoft SQL Server actually).

If there is any way to do this with basic Windows commands that would be preferred as this will be on a production server. We have a lot of controls over the software that we can install in our production environment.

This would be running on Windows Server 2003 at the moment. Although we will be converting to Windows Server 2008 R2 soon.
Question by:LennyGray
    LVL 13

    Accepted Solution

    LVL 10

    Author Comment

    Thanks for the quick response. I do not understand how to clearly identify the file owner from the output.

    I can parse output if needed. But it is not clear how one would parse the output of the calcs command and directly get the file owner. So how can I directly determine the file owner from the CACLS output?

    LVL 11

    Assisted Solution

    Maybe PowerShell script? Installable on Win2003, already installed on 2008 R2:
    $item = Read-Host ("Provide complete path to the target:")
    Get-ChildItem $item | Get-Acl | Select-Object @{n='Path';e={(Get-ChildItem $_.PSPath).FullName}}, Owner

    Open in new window

    LVL 15

    Expert Comment

    You can also use vbscript to do just that.

    'Retrieve owner of a file
    Dim strFilename
    Set args = WScript.Arguments 
    Set objWMIService = GetObject("winmgmts:")
    strFilename = args.Item(0)
    Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting=" & strFilename & """)
    intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)

    If intRetVal = 0 Then
        WScript.Echo "File: " & strFilename
        WScript.Echo "Owner: " & objSD.Owner.Domain & "\" & objSD.Owner.Name
        WScript.Echo "Error retrieving security descriptor for file: " & strFilename"
    End if

    Usage: cscript Owner.vbs c:\temp\somefile.exe

    File: c:\temp\somefile.exe
    Owner: SomeDomain\SomeOwner

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Have you considered what group policies are backwards and forwards compatible? Windows Active Directory servers and clients use group policy templates to deploy sets of policies within your domain. But, there is a catch to deploying policies. The…
    NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now