Windows – Powershell Script to export directory folders and owners (excluding files)

powershellpowershell-v3.0windowswindows-server-2012

I am trying to export all folders including child folders within a file share on a remote server alongside the owner info as shown (Note I do not need info re the files within the folders)

get-childitem \\fileshare\folder -recurse | get-acl  | select-object path,owner,accesstostring,group | export-csv “C:\security.csv”

Whilst this works to a degree it includes all files and not specifically folders within the directory. Note I have also created a temp PS Drive to minimize folder naming length;

new-psdrive -Name X -PSProvider FileSystem -Root \\server\share\folder1\folder2\folder3

Can someone please assist?

Best Answer

Then check for folders with either the -Directory parameter of Get-ChildItem present in recent Powershell versions or use this:

Get-Childitem \\fileshare\folder -Recurse | Where-Object {$_.PSIsContainer} |
  Get-Acl | 
    Select-Object Path,Owner,Accesstostring,Group | 
      Export-Csv “C:\security.csv”

( I prefer a more readable variant of this technical still one liner )