The estimated reading time 2 minutes
Generate a folderstructure with the help of powershell from template with NTFS permissions
some customer asked me to help them creating a special folderstructure with ntfs permission in some subfolders. In my case we want to break inheritance beginning this subfolder.
Have a look at this simple structure
We want to generate a new project inside “folder02” so there should be a folder like project002 containing subfolders “01,02,03” (these subfolders should not have the same permissions from parent folder ) A special user with permissions should generate this as simple as possible.
So this is the challenge. ( Yes I know you should not break inheritance inside a folderstructure so this is not best practices)
So I wrote a little powershell script which asks the name of the folder in a textbox and let the user choose the folder where to generate the new project.
NOTE: you need of course a special user with the appropriated permissions who should execute this script
Enough said so here is my script on github
So have a look on my permission inside this structure
the first step is to change the variables as mentioned in the comments in top of the script (sourcepath in my case is the top folder of the template here C:\files\projecttemplate)
When you start the script it will ask for the name of the folder you want to create
After this it wants to know where to generate the folder and integrate the templates
If the script has finished there should be a folder “project002” containing folders with the same permission as template folders
See the overview of the new folders
The action should also be logged
After testing the script I generated a exe file with my brand new ISEsteroids (http://www.powertheshell.com/isesteroids/) so users can handle it as a normal program.
Give me some feedback concering this script.
Write a comment.
Thank you
Hi!
is this script working recursively when having a bunch of sub folders?
Thanks,
sanni
Hi Sanniso,
yes, the script uses robocopy in the background.
All the best.
Alexander
Hi A.K.,
Thanks for the script, I wonder if it’s possible via powershell, to add a step in the script, which allows the user to choose a subset of the folders in the template eg via a checkbox – folder treeview. I’ve found a solution via Python, but would prefer to use powershell.
Thanks,
K
Hi K.J.
yes this should be possible. Maybe you can use the following lines of code to manage that:
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
SelectedPath = 'C:\'
}
[void]$FolderBrowser.ShowDialog()
I think this should do the trick.
Best regards
Alexander
Hi Alexander,
Thanks, but from what I could find: the basic FolderBrowserDialog does not support a multiselect action.
For my purpose, I would need to have the user make a (multi)-selection of the folders that are in the template folder structure.
Best,
Koen
Hi K.J.,
this would also be possible, but the script itself does not support multiple choices.
See this link for choosing multiple folders:
Best regards
Alexander
I need a Windows 10 powershell script that can MOVE multiple files that are inside multiple folders (in which each file has an extension of “.wmv”), to another single external drive. All these .”wmv” files can keep their existing permissions in their new location, although they can carry temp permissions if this makes it easier.
Is it possible to build such a script possible?
Thnaks so much for your awesome knowledge.
Hi Michael,
thanks for your comment. the easiest way is this kind of one-liners:
get-childitem 'C:\temp\source\' -Include *.xml,*.png -recurse | Move-Item -Destination 'C:\temp\target' -WhatIf
You can try with “whatif” at the end to test it (no permissions are copied)
If you want so retain the securitypermissions robocopy is the easiest way:
robocopy C:\temp\source\ C:\temp\target\ *.xml *.png /MOV /SEC /S
Let me know this helped you or not.
Kind regards