Get-ChildItem
allows you to list files and directories, including recursively with filename filters. Copy-Item
allows you to copy a file.
There is a lot of overlap in terms of selecting the files, often Copy-Item
on its own is sufficient depending on the details of what you need (eg. do you want to retain the folder structure?)
To copy all *.foo
and *.bar
from StartFolder to DestFolder:
Copy-Item -path "StartFolder" -include "*.foo","*.bar" -Destination "DestFolder"
If you need to preserve the folder structure things get harder because you need to build the destination folder name, something like:
$sourcePath = 'C:\StartFolder'
$destPath = 'C:\DestFolder'
Get-ChildItem $sourcePath -Recurse -Include '*.foo', '*.bar' | Foreach-Object `
{
$destDir = Split-Path ($_.FullName -Replace [regex]::Escape($sourcePath), $destPath)
if (!(Test-Path $destDir))
{
New-Item -ItemType directory $destDir | Out-Null
}
Copy-Item $_ -Destination $destDir
}
But robocopy
is likely to be easier:
robocopy StartFolder DestFolder *.foo *.bar /s
In the end the way to choose will depend on the details of what's needed.
(In the above I've avoided aliases (e.g. Copy-Item
rather than copy
) and explicitly use parameter names even if they are positional.)
Best Answer
If you want to copy the entire user's profile folder, that should include the 'Favorites' folder as well. If that is the case, you could use this.
That should create a folder based on the username.