在使用foreach的简单powershell命令复制中需要帮助

| 我是Powershell的新手,这个问题将证明这一点。我正在尝试从命令行执行一个简单的任务,其中有一个txt文件,其中包含以分号分隔的文件名,例如...
fnameA.ext;fnameB.ext;fnameC.ext;....
我正在尝试运行一个命令,该命令将解析此文件,用分号分隔内容,然后对每个文件运行复制命令到所需目录。 这是我正在运行的命令:
gc myfile.txt |% {$_.split(\";\") | copy $_ \"C:\\my\\desired\\directory\"}
但是我在列表中的每个项目上都遇到了这样的错误...
Copy-Item : The input object cannot be bound to any parameters for the command either because the command does not take
 pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:36
+ gc bla.txt |% {$_.split(\";\") | copy <<<<  $_ \"C:\\my\\desired\\directory\"}
    + CategoryInfo          : InvalidArgument: (fileA.txt:String) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand
    
已邀请:
        抵制做出单线的冲动,尤其是当您刚开始时。就是说,问题在于您需要将拆分内容通过管道传输到另一个“ 3”。 尝试这个:
$File = Get-Content .\\MyFile.txt
$File | ForEach-Object {
    $_.Split(\';\') | ForEach-Object {
        Copy-Item -Path \"$_\" -Destination \'C:\\destination\'
    }
}
    
        只需注意:您不需要嵌套每个对象(@Bacon)或使用括号(@JPBlanc),只需使用
Get-Content d:\\test\\file.txt |
  Foreach-Object {$_ -split \';\'} |
  Copy-Item -dest d:\\test\\xx
另请注意,您使用了文件的相对路径,这可能会给您带来麻烦。     
        如果您开始需要发现Powershell CmdLets输出一个对象或对象列表,并且可以在这些对象上使用属性和方法,则@Bacon的建议非常有用。 这是一种较短的方法(出于娱乐目的):
(${c:\\temp\\myfile.txt }).split(\';\') | % {cp $_ C:\\my\\desired\\directory}
    
        
(Get-Content myfile.txt) -Split \';\' | Copy-Item -Destination C:\\my\\desired\\directory
    

要回复问题请先登录注册