Power Shell脚本无法找到C盘

时间:2018-04-04 19:50:34

标签: powershell disk

所以我一直在努力制作一个脚本,将用户和文件",#34;照片" - 文件夹等中的所有文件复制到一个文件夹上另一个驱动器 但剧本一直在说:

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

2 个答案:

答案 0 :(得分:0)

您的问题很可能是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

答案 1 :(得分:0)

我以某种方式使它成功。

这就是代码现在的样子:

$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

非常感谢Shawn Esterman和其他所有人! :)