foreach 循环将文件复制到 c:\users\ 中的所有目录中......希望是一个简单的

foreach loop to copy file into all directories in c:\users\ ... hopefully a simple one

目标:我需要将 Teams 背景复制并粘贴到许多机器上的所有用户配置文件中 错误:路径不存在。 我无法弄清楚为什么我的路径 C:\users\user1\Appdata 变成 C:\Users1 Users2\Appdata

完整代码如下:

$Folders = Get-ChildItem -Directory "C:\Users"   | Where-Object {$_.Name -notlike "Public"}  | Where-Object {$_.Name -notlike "AppData"}
$Backgrounds = "\Server1\Teams-Backgrounds\*"



Foreach($Folder in $Folders) {

Copy-Item -Path $Backgrounds -Destination "C:\Users$($Folders.Name)\AppData\Roaming\Microsoft\Teams\Backgrounds\Uploads" -Force -ErrorAction Stop

}

这就是你想要的。

# Combined the Where-Object filter into a single
$Folders = Get-ChildItem -Directory "C:\Users" | Where-Object {($_.Name -notlike "Public") -and ($_.Name -notlike "AppData")}
$Backgrounds = "\Server1\Teams-Backgrounds\*"

Foreach($Folder in $Folders) {
    # Changed $Folders.Name to $Folder.Name
    Copy-Item -Path $Backgrounds -Destination "C:\Users$($Folder.Name)\AppData\Roaming\Microsoft\Teams\Backgrounds\Uploads" -Force -ErrorAction Stop
}