First, this is my first REAL question on stack-overflow so here we go:
I am trying to use Get-ChildItem and Copy-Item to copy a directory and its contents to every user-profile that may be on a PC without knowing how many users or their usernames. However, I have run into an illegal character issue whenever I run the script or other variations of what I think are the same way to achieve the result. I think it is because of the wildcard but I do not know how I should circumvent that.
For Example: Testing the Wildcard:
Get-ChildItem 'C:Users\*\'
I can see all the users and Power-shell Documentation from Microsoft states using the * should allow it so include all items in that folder. But whenever I extend the path to lets say:
Copy-Item 'D:\Custom Office Templates' -Recurse -Destination 'C:\Users\*\Documents\' -Force
I get an "Illegal Characters in path error." If I replace that * with an actual username to correct the path I get what I want for a single user. The same thing happens when trying to use.
ForEach-Object {Copy-Item -Path 'D:\Custom Office Templates' -Destination 'C:\users\*\Documents\Custom Office Templates' -Force -Recurse}
Thanks you for any assistance given :).
CodePudding user response:
-Destination
is read as a literal path, does not understand about wildcards. What you can do is combine what you already have with Get-ChildItem
using a wildcard on -Path
with a delay-bind script block on -Destination
:
$source = 'D:\Custom Office Templates'
Get-ChildItem 'C:Users\*\' -Directory |
Copy-Item -LiteralPath $source -Recurse -Destination {
Join-Path $_.FullName 'Documents\Custom Office Templates'
}