使用powershell从路径复制文件失败

使用powershell从路径复制文件失败,powershell,file,copy,Powershell,File,Copy,我有这个剧本: $UserFile = Read-Host "Drag your file here" Copy-Item -Path $UserFile -Destination .\input 我希望用户将其文件拖到控制台,以便脚本可以知道用户文件的确切路径,并将用户文件复制到输入文件夹。但我犯了这个错误 Copy-Item : Cannot find drive. A drive with the name '"C' does not exist. At l

我有这个剧本:

$UserFile = Read-Host "Drag your file here"
Copy-Item -Path $UserFile -Destination .\input
我希望用户将其文件拖到控制台,以便脚本可以知道用户文件的确切路径,并将用户文件复制到输入文件夹。但我犯了这个错误

Copy-Item : Cannot find drive. A drive with the name '"C' does not exist.
At line:1 char:1
+ Copy-Item -Path $UserFile -Destination .\input
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
更新: 越来越奇怪的家伙,我认为我的机器有问题,我试着在VM上运行代码,代码运行时没有问题,我的机器和VM之间有一些不同,当我在我的机器上拖动文件时,它会这样写
“C:\path\to\my\file.something”
但是当我在我的虚拟机上这样做时,它会将
C:\path\写入\my\文件。一些不带
的东西

更新: 我刚刚意识到我使用的是不同的文件,我在我的机器上尝试的文件是有空格的
文件一。有些东西
但是我在虚拟机上使用的文件没有空格
文件。有些东西
但是我试着这样运行代码

Copy-Item -Path "C:\path\to\my\file one.something" -Destination .\input

使用
就可以了。但这不是我想要的,我希望用户拖动他们的文件,不管是否有空白。

尝试添加-Prompt参数

$UserFile = Read-Host -Prompt "Drag your file here"
*** drags file ***

Write-Host $Userfile ## should display the filepath

我猜这对你来说已经不是问题了,在拖动你的文件之前,在终端中输入了一个额外的“密码”。对我来说,你发布的代码没有问题

你可能想看看问题出在什么地方


根据此脚本的使用情况,您可以在使用之前修剪/删除任何字符。根据我的经验,除非您明确希望脚本是相对的,否则最好使用绝对路径而不是相对路径(此处为./input)。

我尝试这样运行代码:
Copy Item-Path”“-目的地。\n输入包括
但它可以正常工作。事实上,我不知道问题出在哪里,但我会在找到解决方案后更新这篇文章。谢谢。根据设计,空格、文件名、字段名、文件夹名等必须加引号;这就是为什么应该避免它们,或者需要用下划线等替换空格的原因。这是Windows特有的东西,而不是PowerShell限制。$UserFile的实际值是多少?指向我的文件的路径,类似于
“C:\Path\to\my\file。something”
是否确定目标路径
\input
?如果这不是问题所在,请尝试从收到的文件路径中删除所有引号和周围的空格,然后重新引用。然后使用
LiteralPath
Copy Item-LiteralPath(“{0}”-f$UserFile.Trim(“”)-Destination。\input