Hi I'm struggling mightily with the following - suppose I have the following directory structure C:\Temp\Test1 and C:\Temp\Test2
What I'd like to do is recursively copy the child-contents of C:\Temp\Test1 to C:\Temp\Test2 without copying the actual folder C:\Temp\Test1 ..right now if I use the command
Copy-Item C:\Temp\Test1 C:\Temp\Test2 -Recurse
Will result in C:\Temp\Test2\Test1 and no combination of parameters seems to alleviate the problem
Similarly, when I wish to remove all the child content in C:\Temp\Test2 I wish to only delete the child content and not the actual folder eg
Remove-Item C:\Temp\Test2\ -Recurse
Is removing the \Test2 folder. I've tried so many variations of parameters - how can I accomplish what I am trying to do?
By default when you run the PowerShell Copy-Item cmdlet, it will overwrite the file if it is already exists. Here, we will see an example on PowerShell copy item exclude existing files.
xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.
To copy items in PowerShell, one needs to use the Copy-Item cmdlet. When you use the Copy-Item, you need to provide the source file name and the destination file or folder name. In the below example, we will copy a single file from the D:\Temp to the D:\Temp1 location.
Take a look at the get-childitem command. You can use this in the pipeline to copy or remove all items underneath the root folders:
# recursively copy everything under C:\Temp\Test1 to C:\Temp\Test2
get-childitem "C:\Temp\Test1" | % {
copy-item $_.FullName -destination "C:\Temp\Test2\$_" -recurse
}
# recursively remove everything under C:\Temp\Test1
get-childitem "C:\Temp\Test1" -recurse | % {
remove-item $_.FullName -recurse
}
Copy-Item C:\Temp\Test1\* C:\Temp\Test2
Remove-Item "C:\Temp\Test2\*" -recurse
Works too :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With