How to copy and restore permissions

Discussion in 'Scripting' started by ceo54, Nov 15, 2019.

  1. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    #1 ceo54, Nov 15, 2019
    Last edited: Nov 15, 2019
    Hi,

    I wanted to know, how can I copy and backup the default permissions before making changes to system files, folders etc including most ardent ones like System volume information which I can restore later.

    And while we're at it, how can I obtain the permissions for such objects which do not display current owner like system volume information ?

    Any help will be greatly appreciated.

    Thanks.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,469
    1,357
    120
    Reset Permissions into Right-click menu
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\shell\ResetPermissions]
    "HasLUAShield"=""
    "MUIVerb"="Reset Permissions "
    "NoWorkingDirectory"=""
    "Position"="middle"
    "SubCommands"=""
    
    [HKEY_CLASSES_ROOT\Directory\shell\ResetPermissions\shell]
    
    [HKEY_CLASSES_ROOT\Directory\shell\ResetPermissions\shell\001flyout]
    "HasLUAShield"=""
    "MUIVerb"="Reset permissions of this folder only"
    
    [HKEY_CLASSES_ROOT\Directory\shell\ResetPermissions\shell\001flyout\command]
    @="powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%1\\\" /reset' -Verb runAs\""
    
    [HKEY_CLASSES_ROOT\Directory\shell\ResetPermissions\shell\002flyout]
    "HasLUAShield"=""
    "MUIVerb"="Reset permissions of this folder, subfolders and files"
    
    [HKEY_CLASSES_ROOT\Directory\shell\ResetPermissions\shell\002flyout\command]
    @="powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%1\\\" /reset /t /c /l' -Verb runAs\""
    
    [HKEY_CLASSES_ROOT\*\shell\ResetPermissions]
    "MUIVerb"="Reset Permissions"
    "HasLUAShield"=""
    
    [HKEY_CLASSES_ROOT\*\shell\ResetPermissions\Command]
    @="powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%1\\\" /reset' -Verb runAs\""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\ResetPermissions]
    "MUIVerb"="Reset Permissions"
    "HasLUAShield"=""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\ResetPermissions\Command]
    @="powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%1\\\" /reset' -Verb runAs\""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\ResetPermissions]
    "HasLUAShield"=""
    "MUIVerb"="Reset Permissions "
    "NoWorkingDirectory"=""
    "Position"="middle"
    "SubCommands"=""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell]
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\001flyout]
    "HasLUAShield"=""
    "MUIVerb"="Reset permissions of this folder only"
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\001flyout\command]
    @="powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%1\\\" /reset' -Verb runAs\""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\002flyout]
    "HasLUAShield"=""
    "MUIVerb"="Reset permissions of this folder, subfolders and files"
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\002flyout\command]
    @="powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%1\\\" /reset /t /c /l' -Verb runAs\""
    
    

    or .bat file (Run as administrator)
    Code:
    @Echo Off
    Title Reg Converter v1.2 & Color 1A
    cd %systemroot%\system32
    call :IsAdmin
    
    Reg.exe add "HKCR\Directory\shell\ResetPermissions" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions" /v "MUIVerb" /t REG_SZ /d "Reset Permissions " /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions" /v "NoWorkingDirectory" /t REG_SZ /d "" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions" /v "Position" /t REG_SZ /d "middle" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions" /v "SubCommands" /t REG_SZ /d "" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions\shell\001flyout" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions\shell\001flyout" /v "MUIVerb" /t REG_SZ /d "Reset permissions of this folder only" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions\shell\001flyout\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%%1\\\" /reset' -Verb runAs\"" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions\shell\002flyout" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions\shell\002flyout" /v "MUIVerb" /t REG_SZ /d "Reset permissions of this folder, subfolders and files" /f
    Reg.exe add "HKCR\Directory\shell\ResetPermissions\shell\002flyout\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%%1\\\" /reset /t /c /l' -Verb runAs\"" /f
    Reg.exe add "HKCR\*\shell\ResetPermissions" /v "MUIVerb" /t REG_SZ /d "Reset Permissions" /f
    Reg.exe add "HKCR\*\shell\ResetPermissions" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKCR\*\shell\ResetPermissions\Command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%%1\\\" /reset' -Verb runAs\"" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\*\shell\ResetPermissions" /v "MUIVerb" /t REG_SZ /d "Reset Permissions" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\*\shell\ResetPermissions" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\*\shell\ResetPermissions\Command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%%1\\\" /reset' -Verb runAs\"" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions" /v "MUIVerb" /t REG_SZ /d "Reset Permissions " /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions" /v "NoWorkingDirectory" /t REG_SZ /d "" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions" /v "Position" /t REG_SZ /d "middle" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions" /v "SubCommands" /t REG_SZ /d "" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\001flyout" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\001flyout" /v "MUIVerb" /t REG_SZ /d "Reset permissions of this folder only" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\001flyout\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%%1\\\" /reset' -Verb runAs\"" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\002flyout" /v "HasLUAShield" /t REG_SZ /d "" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\002flyout" /v "MUIVerb" /t REG_SZ /d "Reset permissions of this folder, subfolders and files" /f
    Reg.exe add "HKLM\SOFTWARE\Classes\Directory\shell\ResetPermissions\shell\002flyout\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"Start-Process cmd -ArgumentList '/c icacls \\\"%%1\\\" /reset /t /c /l' -Verb runAs\"" /f
    Exit
    
    :IsAdmin
    Reg.exe query "HKU\S-1-5-19\Environment"
    If Not %ERRORLEVEL% EQU 0 (
     Cls & Echo You must have administrator rights to continue ...
     Pause & Exit
    )
    Cls
    goto:eof
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    Thank you @kaljukass. Highly appreciated. If I'm not wrong, this will reset the permissions of the folders I run this on to the default irrespective of how I mess with the ownership and permissions before ?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,469
    1,357
    120
    #4 kaljukass, Nov 15, 2019
    Last edited: Nov 15, 2019
    It should restore Windows default permissions for folders, subfolder and files. Simple right click to file or folder and then is possible to select for example for only this folder or also for all subfolders and files. This is Windows default function, here is only how to add it to right-click menu to run restore default permissions as simply as possible.
    Screenshots
    [​IMG]
    [​IMG]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    Thanks for clearing it up. Just one more question, will it also revert back the ownership, if I had snatched it from System ? or do I have to manually add that ?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,469
    1,357
    120
    Sorry, now I don't understand anything.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Tiger-1

    Tiger-1 MDL Guru

    Oct 18, 2014
    7,894
    10,736
    240
    yep very useful info a lot users don't know about this...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    Yep I understand I sound confusing. I figured it out. This will reset the permissions not the ownership. I apologies for being annoying. I'm just trying to lean as much as possible. I'm in interested in this stuff.

    I'm highly grateful to you for helping me out. Thank you and God bless.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    Credits to you for bringing this to my attention and the all the members who helped me out in my quest. I'm a hobbyist graphics designer, vectors, artworks, along with Windows themes. I've been messing with system files without any concern for security or stability up until now but not any more. Thanks to to the awesome guys here at MDL.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Tiger-1

    Tiger-1 MDL Guru

    Oct 18, 2014
    7,894
    10,736
    240
    you are welcome :D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,594
    93,453
    340
    icacls will only reset permissions for inherited objects/containters
    if the parent object permissions are changed, i don't think it will restore them to Windows-default
     
  12. Windows_Addict

    Windows_Addict MDL Expert

    Jul 19, 2018
    1,405
    4,753
    60
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    #13 ceo54, Nov 16, 2019
    Last edited: Nov 16, 2019
    (OP)
    @Windows_Addict This is even better. This appears like a complete package with nothing left to do. Also includes in the package are entries to remove the entries from the context menus. Thank you for the help bud. This is very kind of you.

    Edit: Script gives me full control over the files/folders owned by SYSTEM but I couldn't "clear the gained access" that was the original question. There must be something I'm doing wrong.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. Windows_Addict

    Windows_Addict MDL Expert

    Jul 19, 2018
    1,405
    4,753
    60
    @ceo54 in "Clear the gained access" option it restores the permission same as the default of general data files and folders because to restore the original values, a tool must have to store its permission to restore it later and that script doesn't do that work.

    Try NTFS Permissions Tools https://www.majorgeeks.com/files/details/ntfs_permissions_tools.html
    It provides the option to store files permission and you can restore it later.
    I've used it in the past for some time, worked okay.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    906
    402
    30
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...