powershell脚本找不到C磁盘

powershell脚本找不到C磁盘,powershell,disk,Powershell,Disk,所以我一直在尝试制作一个脚本,将用户“文档”、“照片”-文件夹等中的所有文件复制到另一个驱动器上的单个文件夹中。 但剧本一直在说: Copy-Item : Cannot find drive. A drive with the name '‪c' does not exist. At C:\Users\sebbe\Downloads\Untitled1.ps1:23 char:1 + Copy-Item -Path "‪c:\Users\$user\Desktop\*" -Destination

所以我一直在尝试制作一个脚本,将用户“文档”、“照片”-文件夹等中的所有文件复制到另一个驱动器上的单个文件夹中。 但剧本一直在说:

Copy-Item : Cannot find drive. A drive with the name '‪c' does not exist.
At C:\Users\sebbe\Downloads\Untitled1.ps1:23 char:1
+ Copy-Item -Path "‪c:\Users\$user\Desktop\*" -Destination D:\mapp1\$us ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (‪c:String) [Copy-Item], DriveNotFoundExcept 
   ion
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
我以管理员身份通过Power Shell ISE运行该脚本,我在两台不同的PC上尝试了该脚本,但都失败了

为什么脚本找不到C:磁盘

以下是整个脚本:

$User = Read-Host "New Username"

Function makeDir {
    New-Item -Path D:\mapp1 -Name "$User" -ItemType Directory 
}
Function copyDesktop {
    Copy-Item -Path "C:\Users\$User\Desktop" -Destination "D:\mapp1\$User\Desktop" -Recurse
}

Function copyDocuments {
    Copy-Item -Path "‪C:\Users\$User\Documents" -Destination "D:\mapp1\$User\Documents" -Recurse
}

Function copyPictures {
    Copy-Item -Path ‪‪"C:\Users\$User\Pictures" -Destination "D:\mapp1\$User\Pictures" -Recurse
}

makeDir
copyDocuments
copyPictures
copyDesktop

您的问题很可能是PowerShell无法正确扩展字符串。为了确保有一个字符串将路径封装在引号中,如
“C:\users\$User\Documents”
。那么您就不想在路径的末尾使用
\*
。只需使用
Copy Item
的内置
-Recurse
参数即可。如果不使用recurse参数,则可能需要多次运行副本

$user = Read-Host "New Username"

Function makeDir {
    New-Item -Path D:\mapp1 -Name "$user" -ItemType Directory 
}

Function makeDesktop {
    New-Item -Path D:\mapp1\$user\ -Name "$user Skrivbord" -ItemType Directory 
}

Function makeDocuments {
    New-Item -Path D:\mapp1\$user\ -Name "$user Dokument" -ItemType Directory 
}

Function makePictures {
    New-Item -Path D:\mapp1\$user\ -Name "$user Bilder" -ItemType Directory 
} 

Function kopieraDesktop {
    Copy-Item -Path "‪C:\Users\$user\Desktop" -Destination "D:\mapp1\$user\Desktop"
}

Function kopieraDocuments {
    Copy-Item -Path ‪"C:\Users\$user\Documents" -Destination "D:\mapp1\$user\Documents"
}

Function kopieraPictures {
    Copy-Item -Path ‪‪"C:\users\$user\Pictures" -Destination "D:\mapp1\$user\Pictures"
}

makeDir
makeDesktop
makeDocuments
makePictures
kopieraDesktop
kopieraDocuments
kopieraPictures

不知怎么的,我成功了

这就是代码现在的样子:

$User = Read-Host "New Username"

Function makeDir {
    New-Item -Path D:\mapp1 -Name "$User" -ItemType Directory 
}
Function copyDesktop {
    Copy-Item -Path "C:\Users\$User\Desktop" -Destination "D:\mapp1\$User\Desktop" -Recurse
}

Function copyDocuments {
    Copy-Item -Path "C:\Users\$User\Documents" -Destination "D:\mapp1\$User\Documents" -Recurse
}

Function copyPictures {
    Copy-Item -Path "C:\Users\$User\Pictures" -Destination "D:\mapp1\$User\Pictures" -Recurse
}

makeDir
copyDocuments
copyPictures
copyDesktop

非常感谢肖恩·埃斯特曼,当然还有其他所有人!:)

你试过用引号引路径吗<代码>复制项目-路径‪‪'C:\users\$user\Pictures\*'-Destination'D:\mapp1\$user\Pictures\'是的,我尝试了“和”和“什么
得到PSDrive
说?你看到字母C的驱动器了吗?是的,我看到字母C的驱动器了。谢谢你的输入!:@Seb你的脚本很有意义,所以我必须确保它不是愚蠢的东西:(请尝试复制项目-路径“C:\Users\$User\Desktop”-目标“D:\mapp1\$User\Desktop-Recurse我尝试了您的建议,并在主帖子中发布了新代码。但我在尝试复制图片和文档文件夹时仍然收到错误。这是对所有用户还是仅对部分用户发生的?如果输出“C:\users\$user\pictures\”和“D:\mapp1\$user\pictures”,您会得到什么到控制台?生成的字符串是否为有效路径?也就是说,您是否可以导航到它们,运行脚本的用户是否有权访问它们,等等?因为我在两台不同的PC上运行脚本,是的,它对所有用户都有权访问。我确实有权从Power Shell访问路径。:)我觉得很奇怪,因为桌面副本可以工作,而文档/图片却不能。它们在我看来都一样(?)