Search code examples
powershelldirectoryowner

Generating a list of folders on a drive while also including the owner of them. In powershell or batch


I wanted to know if there was a way to find the users name who created a folder on a drive using Powershell or batch. I know DIR pulls up all the folders but it doesn't pull up the owner of the folders. Is there any way for me to do that, both the folders and their owners in 1 command? I have been researching this online and I just haven't been able to find an answer, I feel like this is something really simple.


Solution

  • You can access the owner by using the System.IO.Directory.GetAccessControl method. To get both, you can Add the Owner to directory object and then filter appropriately.

    dir [path] -Directory | ForEach-Object {  
        $acl = [IO.Directory]::GetAccessControl($_.FullName)
        Add-Member -InputObject $_ -NotePropertyName Owner -NotePropertyValue $acl.Owner
        $_
    } | Select-Object FullName, Owner  # etc.