我正在尝试从所有用户个人资料中复制所选文件夹,但显然做错了。你能指出我正确的方向吗?
$destination = "C:\Backup"
$folder = "Desktop",
"Downloads",
"Favorites",
"Documents",
"Music",
"Pictures",
"Videos" ,
"AppData\Local\Mozilla",
"AppData\Local\Google",
"AppData\Roaming\Mozilla"
$computername = gc env:COMPUTERNAME
$userprofiles = Get-ChildItem "C:\Users" -Directory
foreach ($userprofile in $userprofiles) {
Get-ChildItem "C:\Users" -Directory
foreach ($f in $folder) {
$BackupSource = $userprofiles + "\" + $f
$BackupDestination = $destination + "\" + $computername + "\" + $userprofiles + "\" + $f
Copy-Item -ErrorAction silentlyContinue -Recurse -Path $BackupSource -Destination $BackupDestination
}
}
答案 0 :(得分:0)
试试这个,
$destination = "C:\Backup"
$folder = "Desktop",
"Downloads",
"Favorites",
"Documents",
"Music",
"Pictures",
"Videos" ,
"AppData\Local\Mozilla",
"AppData\Local\Google",
"AppData\Roaming\Mozilla"
$computername = gc env:computername
$userprofiles = Get-ChildItem "C:\Users" -directory
foreach ($userprofile in $userprofiles){
foreach ($f in $folder)
{
$BackupSource = $userprofile.FullName + "\" + $f
$BackupDestination = $destination + "\" + $computername + "\" + $userprofile.Name + "\" + $f
Copy-Item -ErrorAction silentlyContinue -recurse -Path $BackupSource -Destination $BackupDestination
}
}
您指的是目录本身,而不是路径。如果它不起作用,请告诉我!
- 根据OP请求进行编辑 -
$userprofiles = Get-ChildItem "C:\Users" -directory
此命令获取C:\ Users下的所有“目录”。请注意,它获取目录本身而不仅仅是路径。 $ userprofiles是一个数组,您必须使用索引(如$ userprofiles [0])或迭代它。这就是你使用foreach ($userprofile in $userprofiles)
做的事情。 (在您的原始代码中,您使用了$ userprofiles,这是数组本身,这就是为什么它不起作用)
好的,现在为什么你需要 $ userprofile.Name 。正如我所说,$ userprofile是目录本身,它具有Name,FullName,CreationTime等属性。我们只是通过$ userprofile.FullName访问它们,它给出了目录的完整路径。