$PWD returns a PathInfo object, instead of a string like you were probably expecting. $PWD.Path
returns a string, but even that could be problematic because you're trying to match for the entire path. If you know ahead of time the source and destinations, and they are both to remain in c:\sites\
, you could do something like this:
$source = "somefolder"
$destination = "anotherfolder"
foreach ($i in Get-ChildItem -Recurse -Force)
{
$path = $i.DirectoryName -replace $source, $destination
$name = $i.Fullname -replace $source, $destination
if(!(Test-Path $path)) { New-Item -Path $path -ItemType directory }
Copy-Item $i $name
}
I made this a little verbose, since you mentioned you're new to powershell. It replaces source content for both a new path and name variable with destination content. It then checks to see if your destination directory exists, creates it if it does not, and then copies the item there.
When you are changing parent directories, you could just include more of the source path in the source string to be replaced. In that circumstance, you would want to use [regex]::Escape($Source)
to escape special characters.