PowerShell:如何仅复制" .exe" ZIP文件夹中的文件到另一个文件夹

时间:2018-05-02 15:44:54

标签: powershell

我有一个" ZIP"文件,当我们提取这个时,我们有1" EXE"文件在4-5子文件夹深度级别内。

我想抓住那个" EXE"文件并复制到另一个文件夹中。如何使用PowerShell做到这一点?

我在下面尝试过,但它会复制所有的ZIP内容,

 $shell = New-Object -ComObject shell.application
 $zip = $shell.NameSpace("Source Path")
 foreach ($item in $zip.items()) {
   $shell.Namespace("Destination Path").CopyHere($item)
} 

1 个答案:

答案 0 :(得分:1)

简单代码段应该可以完成工作

#Sets the variable to the Source folder, recurse drills down to folders within

$Source = get-childitem "C:\Users" -recurse   #"C:\Users" an example

#Filters by extension .exe
$List = $Source | where {$_.extension -eq ".exe"}

#Copies all the items to the specified destination
$List | Copy-Item -Destination "C:\Scripts"   #"C:\Scripts" an example

上面的模块扫描C:\ Users *中的每个.EXE文件并将它们复制到C:\ Scripts

相关问题