Export folder permission using PowerShell

Recently we where moving folder and shares from one server to another. We need to confirm that the folder and permissions were the same on both the old and new share.

To do this I used PowerShell to export the pre and post move permissions and compare the results.

The two commands to get most of the information will be Get-ChildItem and Get-Acl.  The only part of the scripts that will need to be changed is the the export file name to give it a custom name.

Below is the link to full script I will be using.

https://github.com/TheSleepyAdmin/Scripts/tree/master/General/Folder%20Permission

To run the script there will be two mandatory parameters and the command should look like the below. (If you want to look up all subfolders also then just change line 14 and add -Recurse to Get-ChildItem command)

.\Get-FolderPermissions.ps1 -FolderPath \\lab-host01\sources -ExportPath D:\Scripts\Folder_Permissions\Export

I ran the script and changed the exported csv name to pre and post to be used to compare.
Per1
Below is what the export should look like.
Per2
Per3
Once the pre and post export are done we can use compare-object to find any differences.
Just need to update the import-csv paths, I was moving to a share that would have the same FQDN so if that is not the case you can removed the FolderPath from the compare-Object property otherwise all result will not match.
Below is the link to the script I used.
Per4Per5
Below is the export results showing the difference between the pre and post move Per6

10 thoughts on “Export folder permission using PowerShell

  1. Thankyou for a great script, saved me and others a lot of time!

    One question: Can we get this script to show subfolders also? it currently displays the folders under ‘Sources’ but not the subfolders of those folders 🙂 i hope that makes sense

    Thanks again

    Like

  2. Hi PB1882

    yep you can just add -Recurse to the Get-ChildItem

    so should just need to update this line from
    $Folders = Get-ChildItem -Path $Search | Select-Object Name,FullName,LastWriteTime,Length

    to this to get only folders and sub folders with no files.
    $Folders = Get-ChildItem -Path $Search -Recurse -Directory | Select-Object Name,FullName,LastWriteTime,Length

    Like

  3. So quick question about the script… The script is set for “Get-ChildItem”, which gives all items under the actual rootpath I am looking for. When I use \\SERVERNAME\SHARE as my pointer, it doesn’t give me any information on the actual root share, just the information below it. If I change it to “Get-Item”, then it brings up an issue with the “-Directory” parameter, -Recurse parameter, etc.

    How do you suggest changing the script to include the \\SERVERNAME\SHARE root, as well as the Recursed directories?

    Other than that, GREAT script!

    Like

    1. Hi Chuck

      There is no recurse parameter with Get-Item it usually only used for checking item at the root level and Get-ChildItem is used for to do rescusive lookup.

      I thought of a quick workaround there is probable a much better way like doing if statements but I can’t think of how to get it working right now :). So you can just added two new lines to the script

      One to get the Item properties I would add this above the Get-ChildItem part (its line 14 on visual studio code for me)
      $rootfolder = Get-Item -Path $FolderPath | Select-Object Name,FullName,LastWriteTime,Length

      and then combine the two variables by using
      $Folders += $rootfolder
      Do this before the foreach loop (so around line 20)

      I think that should work ok.

      Like

    1. Hi

      For the export to excel it’s a bit difficult to add to the script and I would usually use the export to module from the below link.

      https://github.com/dfinke/ImportExcel

      Then just import the module at the start of the script and change the last line of the script from Export-csv to Export-Excel. On the UTF-8 I think get-childitem should show UTF-8 Characters but i have never had any need for it so not sure.

      Like

Leave a Reply to erkindunya Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s