生成驱动器上的文件夹列表,同时还包括它们的所有者。在 powershell 或批处理中

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

我想知道是否有办法找到使用 Powershell 或批处理在驱动器上创建文件夹的用户名。我知道 DIR 会调出所有文件夹,但不会调出文件夹的所有者。我有什么办法可以做到这一点,文件夹及其所有者都在 1 个命令中?我一直在网上研究这个,我一直找不到答案,我觉得这很简单。

您可以使用 System.IO.Directory.GetAccessControl 方法访问所有者。要获得两者,您可以将所有者添加到目录对象,然后进行适当的过滤。

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