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,362 questions
0 comments No comments
{count} votes

10 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-03-10T09:46:18.817+00:00

    Hi,

    Since you have moved the item to the destination path, you cannot remove it as it no longer exists in the source path. If you want to remove it later you should use copy-item instead
    .

    Get-ChildItem -Path $Source1 -Recurse | Copy-Item -Destination $Target1 -Force  
    Remove-Item -Path $Source1 -Recurse  
    

    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-10T19:42:01.773+00:00

    am now a little bit further:

    with new-psdrive mapping 2 drive letters, 1 for $source and 1 for destination, then do a move-item from source to destination. The move is made for subfolders and files of the sourcce however again the head folder stays on the source.

    \server\share1
    all subfolders of share1 or moved but \server\share1 keeps existing on the server, thats strange to me because a move is a move?

    Another last problem i have is that i want to do this action twice for another source and destination, whats the best thing to do here: like this?:
    $Source = "\server\$naam\"
    $Target="\share\$naam\"
    $Source1= "\server\$naam-ext\"
    $Target1="\share\$naam-\Users\ext-files\$naam-ext\"
    New-PSDrive -Name "Z" -PSProvider "FileSystem" -Root $Source
    New-PSDrive -Name "Y" -PSProvider "FileSystem" -Root $target
    Get-ChildItem -Path $Source | move-Item -Destination $Target -Force
    Get-PSDrive z, y | Remove-PSDrive
    New-PSDrive -Name "Z" -PSProvider "FileSystem" -Root $Source1
    New-PSDrive -Name "Y" -PSProvider "FileSystem" -Root $target1
    Get-ChildItem -Path $Source1 | move-Item -Destination $Target1 -Force

    When the driveletters already excists it givves an error but that i can live with..


  3. corne nietnodig 196 Reputation points
    2021-03-11T18:25:18.183+00:00

    Ill give up tried everything but what i do keep getting is in use by another process, this is also when making a new share on another machine and try invoke-command to remove that shared folder, same error.
    I believe it is not possible simply remove share remotely without giving the whole path?

    0 comments No comments

  4. corne nietnodig 196 Reputation points
    2021-03-11T19:01:51.61+00:00

    tried net share, remove-smbshare but they say the share does not excist....although i can remotely go to the share and it is on the server....

    Is there somewhere a setting maybe that foldershares cannot be removed...remotely

    0 comments No comments

  5. Robert Davidian 41 Reputation points
    2022-08-11T06:30:00.667+00:00

    This article may help. Robocopy wrapper for Powershell!

    https://adam-bacon.netlify.app/recent-modules/robocopy/

    0 comments No comments