Search code examples
windowspowershellaclwindows-server-2008-r2powershell-5.0

Retrieve full path and file permissions of file modified between date range in powershell


Considering this is my first post EVER on stackoverflow, and quite new to PowerShell; I would request you all to be a bit gentle with your replies.

I am trying to get the contents modified between a date range for a HUGE production server. I want the contents to be exported to a .csv file with the columns representing the FullName, LastWriteTime, User/Group, Permissions of the associated folder housing the modified file, and if it has been inherited.

I have gone through TONS of forums, articles etc. but haven't been able to get the desired output. Here is the code I have been trying to run:

    $BaseFolder = "<path to be traversed>"

[datetime]$start = '2019-10-05 00:00:00'
[datetime]$end = '2020-10-25 00:00:00'

$Acl = Get-Acl -Path $BaseFolder

#For each Group or Username in ACL   
$Files = ForEach ($Access in $Acl.Access){

Get-ChildItem -Path $BaseFolder -Recurse -Force | Where-Object { $_.LastWriteTime -gt $start -AND $_.LastWriteTime -lt $end } | 
Select-Object #FullName, LastWriteTime
              @{Name='Path';Expression={$_.FullName}},
              @{Name='LastWriteTime';Expression={$_.LastWriteTime}},
              @{Name='Group/User';Expression=$Access.IdentityReference},
              @{Name='Permissions';Expression=$Access.FileSystemRights},
              @{Name='Inherited';Expression=$Access.IsInherited}

} 

$Files | Export-Csv "<save-path>\Folder-Permissions-$((Get-Date).ToString("yyyy-MM-dd_HHmmss")).csv" -NoTypeInformation

Here are some screenshots for a better idea:

Current output:
Although this does generate some output, it is not what I desire.

Desired output:
I desire an output similar to this(I have heavily edited this screenshot)

Any help is highly appreciated, and a big thank you in advance!

Regards,

Manny


Solution

  • I suppose it's the comment after select-object that messed up everything, because there's a linebreak after and the hashtables will not be passed as arguments to the command. Also there was an issue in your Expression definitions (you have to put them in a script block {}).

    This should work:

    $BaseFolder = "<path to be traversed>"
    
    [datetime]$start = '2019-10-05 00:00:00'
    [datetime]$end = '2020-10-25 00:00:00'
    
    $Acl = Get-Acl -Path $BaseFolder
    
    $Files = ForEach ($Access in $Acl.Access){
        Get-ChildItem -Path $BaseFolder -Recurse -Force |
            Where-Object { $_.LastWriteTime -gt $start -AND $_.LastWriteTime -lt $end } | 
            Select-Object @{Name='Path';Expression={$_.FullName}},
                          @{Name='LastWriteTime';Expression={$_.LastWriteTime}},
                          @{Name='Group/User';Expression={$Access.IdentityReference}},
                          @{Name='Permissions';Expression={$Access.FileSystemRights}},
                          @{Name='Inherited';Expression={$Access.IsInherited}}
    } 
    
    $Files | Export-Csv "<save-path>\Folder-Permissions-$((Get-Date).ToString("yyyy-MM-dd_HHmmss")).csv" -NoTypeInformation