VBS Ownership & Copy

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 1
Last visit: Sun Feb 17, 2013 7:10 pm

VBS Ownership & Copy

Post by lloydie2009 »


I am trying to create a VBS to take ownership of a user input parameter folder, and then copy that to another location however am having issues.

I think it's the ownership bit that causing a problem which if run in a single script works fine, and is only when trying to add the robocopy section of the script I am having issues.

I'm afraid I have limited knowledge, and there is no error that appears, it prompts for the username as it should, and then closes a few moments later, it would appear when it hit's the robocopy section, however if you then look at c:users, ownership has not been taken.

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strsUsername = InputBox ("please enter target username")

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "takeown.exe /F C:users"&sUsername&""

objShell.Run "robocopy.exe c:users"&sUsername&" c:localprofilebackuplocalprofile /e /zb /R:3 /W:2"

Any suggestions on how i can accomplish this would be great, i need to have a InputBox as this is for Admin to use from different accounts, so the script cannot auto-determine the folders.

Many Thanks

Posts: 14759
Last visit: Thu Apr 15, 2021 5:14 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

VBS Ownership & Copy

Post by jvierra »

Hi lloyd,

There are a couple of things her3e that will give you trouble. YOU do not allow any space around the & signs which is required and the re is no need to add an empty string to the end.

Only an admnistrator can run this command.

Try the following.

strsUsername = InputBox ("please enter target username")

Set objShell = CreateObject("Wscript.Shell")
strCmd = "takeown.exe /F C:users" & sUsername
ret = objShell.Run( strCmd, 1, True )
if ret = 0 Then
strCmd = "robocopy.exe c:users" & sUsername & " c:localprofilebackuplocalprofile /e /zb /R:3 /W:2"
ret = objShell.Run( strCmd, 1, True)
msgbox ret
MsgBox "Command failed:" & ret
End If