Alright, been working on this for hours and researching like crazy, but still not getting something to work. I need a string[] object created from get-childitem to pass to the Copy-Item -exclude parameter.
The hurdle is that I need to do recursion and need to have relative paths, so this is what I came up with:
$((Get-ChildItem -Path $Dest -Recurse -File).FullName.TrimStart($Dest))
This results in a clean list of existing files in $dest that are presented with a relative path to $dest. The problem is, if I add this to the copy-item -exclude parameter it seems to ignore it. Further research online suggests that copy-item will ignore the -exclude parameter if it is not of type string[].
If I check the type returned by the above command, I get System.Object[]. I expect it to be System.String[] or just plain String[].
How do I convert the output of the above command to a string array?
The full command using copy-item, for clarity, is:
Copy-Item -Path (Join-Path $src "*") -Destination $dest -Recurse -Force -Exclude $((Get-ChildItem -Path $Dest -Recurse -File).FullName.TrimStart($Dest))
My end goal is to copy files recursively without overwriting existing files.
-Exclude(and-Include) only support file name patterns, not paths. However, adding support for paths is being discussed in this GitHub issue.TrimStart()is supposed to be used. It takes an array of characters for which all occurrences will be removed from the beginning of the string, not an exact string to be removed from the beginning; for that you'd want something like.FullName.Substring($Dest.Length). ApparentlyTrimStart($Dest)works for you here, which suggests$Destdoes not contain a trailing backslash. With a trailing backslash it might remove characters from the relative child path beyond the length of$Dest.'ababab'.trimstart('ab').