Permission on folders and disk space usage

Hello all,

I am trying to create a script to get a list of permissions on a folder share, and disk space used by each of those folders.

The thing is that either i just get listed the root path, or every single subfolder, but i just need the first level of folders included in the report.

For sure it is a very simple script, but i’ve been on this for over a week without success.

Any help will be very much appreciated!!

Thanks in advance

Hi, welcome to the forum :wave:

Please share the code you have so far.

Hi,

This is what i am working on…

$Ruta = "\\Server01\Contabilidad"
$Informe = ".\Informe.csv"

Function Tamanho
{
Param
(
[Parameter (Mandatory=$False)][String]$RutaPermisos,
[Parameter (Mandatory=$False)][Array]$Reporte
)

$Subfolders = Get-ChildItem -Path $RutaPermisos -Directory
$TamanoCarpeta = "{0:N2}" -f((Get-ChildItem $RutaPermisos | Measure-Object -Property Length -Sum).Sum/1GB)
$FolderAccess = (Get-Acl -Path $RutaPermisos).Access

Foreach ($Access in $FolderAccess)
{
$Reporte = $Reporte +"$($RutaPermisos),$($TamanoCarpeta),$($Access.IdentityReference),$($Access.FileSystemRights)"
}

If ($Subfolders.count -ne 0)
{
Foreach ($SubCarpeta in $Subfolders)
{
$Reporte = Tamanho -RutaPermisos $SubCarpeta.FullName -Report $Reporte
}
}
Return $Reporte
}

$Reporte = @()
$Reporte = $Reporte+"Carpeta,Usuario,Permisos,Heredado,Tamano"
$Reporte = Tamanho -RutaPermisos $Ruta -Report $Reporte
$Reporte | Add-Content -Path $Informe

When posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code.

Please edit your post to format the code:

How to Format Code on PowerShell.org

I hope i did what you needed and is better to understand now

Thanks a lot for your guidance

Thanks for reformatting that makes it much clearer.

It would be better to output objects, rather than generate strings. You will have more flexibility in what you do with the output.

I also don’t think calling the function recursively is necessary as you said you only want information about the first level of folders. This would be my approach:

function Get-FolderInfo {

    param (
        [String]$rootFolder
    )

    $firstLevelFolders = Get-ChildItem -Path $rootFolder -Directory

    foreach ($folder in $firstLevelFolders) {
        $folderSize = Get-ChildItem -Path $folder -Recurse -File | Measure-Object -Property Length -Sum | Select-Object -ExpandProperty Sum
        $folderSize = [Math]::Round($folderSize/1GB,2)
        $folderAccess = Get-Acl -Path $folder | Select-Object -ExpandProperty Access        
        foreach ($access in $folderAccess) {
            [PSCustomObject]@{
                Name              = $folder.Name
                Size              = $folderSize
                IdentityReference = $access.IdentityReference
                FileSystemRights  = $access.FileSystemRights
            }
        }
    }
}

Get-FolderInfo -rootFolder E:\Temp | Export-Csv E:\Temp\FolderReport.csv
1 Like

Thanks a lot for your version. I tried running it as is, but get a 0 lenght output file created. Were you able to test it with good results?

Yes, I get the expected output.

1 Like