question

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 asked RobertDavidian-1204 answered

Robocopy in powershell variablen

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered cornenietnodig-5642 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi IanXue, what am i doiing when putting this in it:
do { $naam = Read-Host -Prompt "Please enter user logon name" }
while(-not $naam)
robocopy (Join-Path $src $naam) (Join-Path $dest $naam) /E


For example when i read it it says: while -not $naam, i read when it is not $naam?

What is join path is that a move item?


0 Votes 0 ·
cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered cornenietnodig-5642 edited

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered cornenietnodig-5642 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi IanXue,

I keep getting cannot remove-item \\server\share because it is in use by another process. I have made a new share on the source en filled it with files and a subfolder.
The copy of the first $source is done but not the remove-item because it is in use with another process. this is also the case when making a new share...
The second copy-item after $source1 and target1 are not working at all, it is not making a copy and does not remove-item because of the same reason.

Any idea what am i doiing wrong? I have the rights to delete the folders and files, when i do this manually it is no problem to delete them.

0 Votes 0 ·
cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered cornenietnodig-5642 commented

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..

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The only question left is why does the root folder share not beiing moved or deleted, when i do a remove-item it says that it is in use by another process?

The foldershare is empty all files and subdirs are moved..i have tried:
rdir \\server\share
rd...
remove-item ...


If i could i would try the complete path to the #source to delete it but this is not automated then because i do not now in advance the compete path because behind the scened \\server\share is in reality \\server\e$\subdepartment\$naam
I do not know subdepartment in advance.

0 Votes 0 ·
cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RobertDavidian-1204 avatar image
0 Votes"
RobertDavidian-1204 answered

This article may help. Robocopy wrapper for Powershell!

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.