none
VBScript to take ownership of subfolders/files

    Question

  • Hi guys,

    I'm looking for a VBScript to change the ownership of subfolders/files of user home directories. I can do it manually but I have over 3000 users in the network and that would cost me a huge amount of time. So there for i need to create a script that does this for me.

    I will be honest, I 'm a total noob in scripting.

    kind regards,

    Richard

    Wednesday, March 20, 2013 4:01 PM

Answers

  • Hi guys,

    I'm looking for a VBScript to change the ownership of subfolders/files of user home directories. I can do it manually but I have over 3000 users in the network and that would cost me a huge amount of time. So there for i need to create a script that does this for me.

    I will be honest, I 'm a total noob in scripting.

    kind regards,

    Richard

    Group Policy can add teh Administrator group to all user home folders asuming you have set up the AD and users correctly for GP and AD.  If you are using NT4 methods then you will need to take extra steps.

    You can use WMI to take ownership of files and folders on remote systems:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa393903(v=vs.85).aspx


    ¯\_(ツ)_/¯

    Wednesday, March 20, 2013 4:23 PM
  • This is actually very easy to do in PowerShell,

    $userfolder='e:\\folder\\user'
    $folder=gwmi win32_directory -filter "name='$userfolder'" -computer pc001
    $folder.TakeOwnerShipEx($null,$true)

    This will takeownership of all files and folders from the root down.

    The path has to be a physical path on the machine where the users home directory is located.  If this is available in AD then it is pretty easy to script.


    ¯\_(ツ)_/¯


    Wednesday, March 20, 2013 4:35 PM
  • This depends on what scripting language you want to use. If you want to use a shell script (batch file), you can use the for /f command to read the lines of text from a file and run a command (or call a subroutine) for each line. Run for /? at a cmd.exe prompt for more information.

    If you want to use VBScript, you can use the FileSystemObject object's TextStream object to open and read the lines of text from a file.

    If you're not familiar with how to write scripts in VBScript, I recommend the VBScript primer in the Windows 2000 Scripting Guide.

    Bill

    Thursday, March 21, 2013 2:16 PM
  • Go here:

    http://technet.microsoft.com/en-us/library/ee198742.aspx

    This is the chapter on using the FileSystemObject in the Windows 2000 Scripting Guide.

    Bill

    Thursday, March 21, 2013 2:53 PM

All replies

  • Have you searched the web and the script repository?

    Bill

    Wednesday, March 20, 2013 4:06 PM
  • Hi guys,

    I'm looking for a VBScript to change the ownership of subfolders/files of user home directories. I can do it manually but I have over 3000 users in the network and that would cost me a huge amount of time. So there for i need to create a script that does this for me.

    I will be honest, I 'm a total noob in scripting.

    kind regards,

    Richard

    Group Policy can add teh Administrator group to all user home folders asuming you have set up the AD and users correctly for GP and AD.  If you are using NT4 methods then you will need to take extra steps.

    You can use WMI to take ownership of files and folders on remote systems:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa393903(v=vs.85).aspx


    ¯\_(ツ)_/¯

    Wednesday, March 20, 2013 4:23 PM
  • This is actually very easy to do in PowerShell,

    $userfolder='e:\\folder\\user'
    $folder=gwmi win32_directory -filter "name='$userfolder'" -computer pc001
    $folder.TakeOwnerShipEx($null,$true)

    This will takeownership of all files and folders from the root down.

    The path has to be a physical path on the machine where the users home directory is located.  If this is available in AD then it is pretty easy to script.


    ¯\_(ツ)_/¯


    Wednesday, March 20, 2013 4:35 PM
  • Thanks for the answers. I think i found the right command to do this. In order to change the owner of the subfolders/files I need to run this command "Icacls /setowner D:\data\userhome\$somevariable domainname\$somevariable /t"

    And in order to change the owner of the home folder back to the domain admins I need to run this command "Icacls /setowner D:\data\userhome\$somevariable domainname\administrators"

    To run this in a VBScript, I need to copy the list of users in a text file (for example) and create a VB script that opens and reads the text file in a variable ($somevariable).

    Not sure how to create a script like this.

    Thursday, March 21, 2013 11:36 AM
  • This depends on what scripting language you want to use. If you want to use a shell script (batch file), you can use the for /f command to read the lines of text from a file and run a command (or call a subroutine) for each line. Run for /? at a cmd.exe prompt for more information.

    If you want to use VBScript, you can use the FileSystemObject object's TextStream object to open and read the lines of text from a file.

    If you're not familiar with how to write scripts in VBScript, I recommend the VBScript primer in the Windows 2000 Scripting Guide.

    Bill

    Thursday, March 21, 2013 2:16 PM
  • I don't know how to create the connection between the vbscript and the text file using FSO. I found this on some website yesterday:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\FSO\New Text Document.txt", 1)
    Do Until objFile.AtEndOfStream
        strCharacters = objFile.Read(1)
        Wscript.Echo strCharacters
    Loop
    

    I have actually no idea what it does. And If this might be right script to run a text file in a vbscript then how do I create a for to execute the icacls? I was thinking of something like this:

    : Begin
    
    'here should be the script to read the text file(FSO code)'
    -openfile(textfile.txt) 
    
    'here comes the loop, but i don't know how to make it read until the last line'
    - FOR i = 1 to last line??(textfile.txt)
      Read line 1(somehow)
      $userfolder = Read line 1
    icacls /setown D:\Data\userhome\$userfolder PC001\$userfolder /t
    
    icacls /setown D:\Data\userhome\$userfolder PC001\admins
    
    Nextline
    
    next
    
    end

    I hope you can understand what I'm trying to achieve here, it's really hard for me because I've never done this before.

    Thanks a lot.


    Thursday, March 21, 2013 2:50 PM
  • Go here:

    http://technet.microsoft.com/en-us/library/ee198742.aspx

    This is the chapter on using the FileSystemObject in the Windows 2000 Scripting Guide.

    Bill

    Thursday, March 21, 2013 2:53 PM
  • Hi Bill,

    I've tried to watch some tutorials on youtube the last couple of days, but until now I haven't learned anything about scripting. It just doesn't go in haha.

    Tuesday, March 26, 2013 8:09 AM
  • FOR /F "usebackq tokens=1 delims=" %i IN ("%userprofile%\desktop\computername.txt") DO @echo icacls /setown "D:\Data\userhome\%i" "PC001\admins"

    Remove the @Echo to actually run the icacls command. As above it prints the icacls command rather than execute it.

    Put the folder names one per line into a text file called computername.txt on your desktop.


    David Candy

    Tuesday, March 26, 2013 8:38 AM