I'm trying to copy a file to a list of users' folders on a network share using a foreach loop and a variable containing all of the user names. This is what I've done so far with no success. I understand why it's not working (loading all usernames at once in the variable), but I'm not sure what to do to correct it, or if a foreach loop is the best route to take. I've tried a lot of variations of the below and done a lot of googling.
$whusers = "username1", "username2", "username3"
Foreach ($user in $whusers) {
xcopy "C:\users\myname\desktop\shortcut.url" "F:\corp users\$whusers\desktop" }
Any help would be great appreciated!
When using a ForEach loop, you are iterating through all elements of the 2nd variable and putting them in turn into the first. Inside the ForEach, you must reference the first variable, not the second. Change the $whusers
inside your ForEach to $user$
.
Try this:
$whusers = "username1","username2","username3"
Foreach ($user in $whusers) {
xcopy "C:\users\myname\desktop\shortcut.url" "F:\corp users\$user\desktop"
}