1

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.

Dpw808
  • 119
  • 1
  • 3
  • 10

1 Answers1

1

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.
Cobster
  • 1,243
  • 10
  • 18
  • You can call `GetAccessControl` directly on the `DirectoryInfo` instance: `$_.GetAccessControl().Owner` – Mathias R. Jessen Feb 29 '16 at 19:19
  • i tried the above thing and got this: `Method invocation failed because [System.RuntimeType] does not contain a method named 'GetAccessControl'. At line:2 char:5 + $acl = [IO.Directory].GetAccessControl($_.FullName) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound` – Dpw808 Feb 29 '16 at 19:19
  • Bad syntax, my bad. Fix `.` with `::` – Cobster Feb 29 '16 at 19:21
  • I am trying to get the folders and owners from a network drive, is there anything else I need to do differently because of that? – Dpw808 Feb 29 '16 at 19:21
  • Great it worked! now how can I get that and the folder names to line up in the command? the folder names don't get printed with the solution you gave. – Dpw808 Feb 29 '16 at 19:24
  • Added the code that will allow you to get it all at once. – Cobster Feb 29 '16 at 19:25
  • I just tried it, i got no errors but also no output. – Dpw808 Feb 29 '16 at 19:26
  • My bad again, getting ahead of myself. Needed to add $_ to return object from ForEach-Object pipeline. – Cobster Feb 29 '16 at 19:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104911/discussion-between-dpw808-and-cobster). – Dpw808 Feb 29 '16 at 19:40