Robocopy as another user

12,928

Solution 1

Robocopy will use the standard windows authentication mechanism.

So you probably need to connect to the servers using the appropriate credentials before you issue the robocopy command.

You can use net use to do this.

net use X: '\\Source\E$\Location' /user:MYDOMAIN\USER THEPASSWORD
net use Y: '\\Destination\Location Here' /user:MYDOMAIN\USER THEPASSWORD

net use X: /d
net use Y: /d

and then start your ROBOCOPY

Solution 2

S.Spieker's answer will work, but if you want to use PowerShell built in command and pass the credentials as a pscredential object you could use New-PSDrive to mount the drives:

    $passw = convertto-securestring "Password" -asplaintext –force
    $creds = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username", $passw
    $SourceFolder = '\\Source\E$\Location'
    $DestinationFolder = '\\Destination\Location Here'

    New-PSDrive -Name MountedSource -PSProvider FileSystem -Root $SourceFolder -Credential $creds
    New-PSDrive -Name MountedDestination -PSProvider FileSystem -Root $DestinationFolder -Credentials $creds

    Robocopy.exe \\MountedSource \\MountedDestination /e /Copy:DAT"

    Remove-PSDrive -Name MountedSource 
    Remove-PSDrive -Name MountedDestination 

* I might have the Robocopy wrong, it's been years since I used it, but the mounting drives is correct.

Share:
12,928
Drew
Author by

Drew

Write code for practical uses and to save me time. It may not be pretty but it saves me the hassle of actually clicking buttons. Why add 3 users to a distribution group in EMC when you can spend 20 minutes writing up a GUI to add them and have a sick background image.

Updated on August 16, 2022

Comments

  • Drew
    Drew almost 2 years

    Problem: Robocopy not launching as another user in Start-Process

    The script works fine when running on an account that has the permissions for both file locations but it just doesnt seem to be accepting the -credential param.

    Unsure if my formatting is incorrect or if I am doing something wrong.

    # Create Password for credential
    $passw = convertto-securestring "Password" -asplaintext –force
    # Assembles password into a credential
    $creds = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username", $passw
    # Select a source / destination path, can contain spaces
    $Source = '\\Source\E$\Location'
    $Destination = '\\Destination\Location Here'
    # formats the arguments to allow the credentials to be wrapped into the command
    $RoboArgs = "`"$($Source)`" `"$($Destination)`"" + " /e /Copy:DAT"
    # Started Robocopy with arguments and credentials
    Start-Process -credential $creds Robocopy.exe -ArgumentList $RoboArgs -Wait