PowerShell implementation of xcopy - command-line

Is there a way to replicate an xcopy functional using powershell?
I thought it was an easy question until I tried some cmdlets.
Let's imagine I've got a folder structure like:
src
|-a
|-b
There're files in each folder of course. I need to copy contents of Src to some folder Dst.
With xcopy it'd be like this:
xcopy src dst\ /e /y
PS analog would be something like this:
copy-item src dst\ -force -recurse -verbose
Works great... the first time. The second time it creates a subfolder dst\src and puts files there!
I can't figure out any easy workaround. Can you?
p.s. I know I can use xcopy in PS.

copy-item c:\\src\\* c:\\dst -force -recurse -verbose

Here's another workaround I found here:
https://github.com/nightroman/PowerShellTraps/tree/master/Cmdlets/Copy-Item/Inconsistent-destination I've tried in the past to do this myself and would revert back to xcopy. This is "idempotent" and will have the same result the second time.
mkdir $destination -Force
Copy-Item $source\* $destination -Recurse -Force

Related

Fastest approach to copy millions of JPGs to destination with Robocopy and Powershell?

I want to copy all JPGs within a directory (including subfolders) to a target destination (flat hierarchy).
My Powershell Script seems super slow :(
Any ideas how to make the fastest approach? Is robocopy a faster solution?
It seems that Robocopy always also creates the folder of the JPG. But I want it flat like
C:\destination\image1.jpg
C:\destination\image2.jpg
C:\destination\image3.jpg
Is it possible that only images are copied that arent already in the target folder?
Get-ChildItem -Path "R:\XXX\*" -Include *.jpg -Recurse | Copy-Item -Destination $targetpath -verbose

robocopy copying parent folders

I have a directory of photos that are placed in folders named with the date each photo is taken. I want to move all photos to another folder.
robocopy 'source' 'target' *.jpg /s copies the parent date folders as well. how to stop this from happening?
Try something like this in native powershell:
Copy-Item -Path C:\source -Filter *.jpg -Destination c:\target –Recurse
basically, to answer your question. its because of the /s parameter

PowerShell Copy-Item not copying all files

I have a strange issue with the Copy-Item in PowerShell. Below you see two almost identical lines of code, only the source and destination is different.
Copy-Item "A:\*" -Destination "B:\" -Recurse -Force -Verbose -Confirm:$false -ErrorAction SilentlyContinu
Start-Sleep 3
Copy-Item "B:\*" -Destination "D:\" -Recurse -Force -Verbose -Confirm:$false -ErrorAction SilentlyContinu
These lines are a part of a script. It first notes the date and creates a log file and then it clears the B and D drive. After that, it starts copying. The first one, the Copy-Item A:\ to B:\ works, it copies everything.
When PowerShell gets to the second one, it only copies a few folders and just skips over the remaining folders. It doesn't throw me an error or anything.
When I manually execute the second line, it works, but not when it's in a script.
What is wrong here?
I'd use robocopy for this, it's included in all modern versions of windows and is very powerful and can complete pretty much any copy function you want.
ROBOCOPY /MIR <Source> <Target>
/MIR is the mirror switch. It will both copy source to target and delete anything in target that isn't present in source. Leaving target as a mirror copy of source.
You might also want to look into the /MT multi-thread switch to speed things up if the directories contain a large number of files.

Move-Item from sub folder to second subfolder using variable destination

I'd like to move all the files in multiple folders into a subfolder within those folders.
So from C:\Desktop\Folder to C:\Desktop\Folder\cv.
I'm having difficulty targeting the correct destination, so far I've tried:
Get-ChildItem *cv -Recurse -Exclude "cv" | Move-Item -Destination {"C:\Desktop\$($_.Name)\cv"}
I'm using Windows PowerShell.
EDIT: For those interested I've found a way to do this using CMD:
FOR /D %G IN ("*cv") DO robocopy /move "%~G" "%~G\cv" "*"
I'd still like to know how to do this using PowerShell, though.

Powershell/bat - copy folder to multiple destinations

I have folder c:\tocopy that I would like to copy the content to c:\dest1, c:\dest2 and overriding the existing files
I am not sure what my script would need to look like to be able to do this.
(using win2k12)
Thank you
would use robocopy for this purpose.
http://www.windows-commandline.com/robocopy-switches-syntax-examples/
http://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx
http://improve.dk/simple-file-synchronization-using-robocopy/
In this particular case:
robocopy c:\tocopy c:\dest1 /MIR /Z
robocopy c:\tocopy c:\dest2 /MIR /Z
/MIR makes dest mirror tocopy
/Z makes it so that the copy is restartable (depending on the size of the things you are copying you may not need this, but you probably want it when transferring files over the network or doing large copies
To use powershell, try the copy-item cmdlet:
Copy-Item c:\tocopy -Destination c:\dest1 -Recurse -Force
Copy-Item c:\tocopy -Destination c:\dest2 -Recurse -Force