Robocopy in powershell variablen

corne nietnodig 196 Reputation points
2021-03-08T08:11:07.877+00:00

Hi, I would like to move (or copy and then delete) a folder from a network share to another networkshare with Powershell but cannot get it to work with get-item, move-item, remove-item etc, because getting access denied all the time while i am an administrator of the domain so thatswhy i would like to use another method in Powershell to make a copy of a folder on a networkshare to another networkshare with the name of the user.
The name of the user is a variable which is asked during execution and based on the input the folder must be moved from and to the \server\share\nameuser. Can

Robocopy move or copy delete when using the variable as a name, for example (this does not work: Robocopy cannot see the input $naam when given in):

param ($naam) if ($naam -eq $null){ $naam = Read-Host -Prompt "Please enter user logon name" } Set src="\server\$naam" Set dest="\server\Users" robocopy %src% %dest% /E

I would prefer to use Powershell but i can copy-item with Powershell then the folder is copied but remove dir or remote-item does not work: access denied although i have enough rights.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,454 questions
0 comments No comments
{count} votes

10 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 34,116 Reputation points Microsoft Vendor
    2021-03-08T10:12:10.067+00:00

    Hi,

    You can call robocopy in the powershell script like this.

    $src = "\\server\share\"  
    $dest = "\\server\Users\"  
    do { $naam = Read-Host -Prompt "Please enter user logon name" }   
    while(-not $naam)  
    robocopy (Join-Path $src $naam) (Join-Path $dest $naam) /E  
    

    Make sure you have full control permission to the $dest if you want to remove the $naam directory.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. corne nietnodig 196 Reputation points
    2021-03-08T14:55:54.257+00:00

    Lets say i would like to move the folder (or first copy it and then delete it):

    \server\sharename (is $naam) so the source is a folder named: \server\folder\$naam
    destination is: \server\users\$naam

    $naam is a variable and i will fill that name in everytime i would doe this move or delete on the source \server\$naam

    How would i do this with robocopy or powershell when that works without acccess denied?

    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 34,116 Reputation points Microsoft Vendor
    2021-03-09T08:16:26.127+00:00

    Hi

    The do...while loop will read your input to $naam when you do input something, not just press "enter". Join-Path combines $dest and $naam into a single path "\server\users\$naam". To move the folder you can use move-item

    $src = "\\server\folder\"  
    $dest = "\\server\Users\"  
    do { $naam = Read-Host -Prompt "Please enter user logon name" }   
    while(-not $naam)  
    Move-Item -Path (Join-Path $src $naam) -Destination (Join-Path $dest $naam)   
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. corne nietnodig 196 Reputation points
    2021-03-09T14:07:36.787+00:00

    t is almost working, i have something like above and added another source like source1 and a new target target1. I have tested the move without source1 and target1 but i would really built in a check that the move succeeded before the commands remove-item, because when the move failes and the scripts goes on then the folder is removed without the move.

    I have tested above with source and target, the move is done for all subfolders and files but the \server\$naam keeps excisting so is not removed, thatswhy i use the remove-itm to delete the foldershare.

    param ($naam)
    if ($naam -eq $null){
    $naam = Read-Host -Prompt "Please enter user logon name"
    }
    $Source = "\server\$naam\"
    $Target="\share\$naam\"
    $Source1= "\server\$naam-ext\"
    $Target1="\share\$naam-\Users\ext-files\$naam-ext\"
    Get-ChildItem -Path $Source | move-Item -Destination $Target -Force
    Remove-Item $Source
    Get-ChildItem -Path $Source1 | move-Item -Destination $Target1 -Force
    Remove-Item $Source1

    0 comments No comments

  5. corne nietnodig 196 Reputation points
    2021-03-09T18:49:42.57+00:00

    I have tested above but for some reason after:
    Get-ChildItem -Path $Source1 | move-Item -Destination $Target1 -Force

    It moves the files in the share -ext but not the subfolders so i must use (i think)
    Get-ChildItem -Path $Source1 | move-Item -Destination $Target1 -Force -recurse

    After the move it cannot execute:
    Remove-Item $Source1

    Because it says: cannot remove item cannot get-access to the file because it is in use by another process

    Nothing is using that folder share because it is a test folder. So the reason for this is probleby the move-item before this command.

    How can i solve this?

    The goal is to completely disable user, edit the description in AD en resert its password. (this is taken care of) after this above must be activated that is:
    remove the filesshares of the users and i believe for me it is easyest to move the shares to another folder and then remove the share AND all it's subfolders and file.

    0 comments No comments