Skip to main content
 首页 » 编程设计

powershell之如何在powershell中移动文件夹及其内容

2025年12月25日37lexus

我正在尝试将一个文件夹及其内容从一个位置移动到另一个位置。
文件夹的名称是 c:\logfiles 并且它有子文件夹和文本文件,但是我编写的脚本只移动了日志文件的内容,但我想将日志文件文件夹作为一个整体移动到一个新位置

$current_logfiles = 'C:/LogFiles/*' 
$new_logfiles = 'C:\My_Project\LogFiles\' 
 
if (!(Test-Path -path $new_logfiles  )) { 
New-Item $new_logfiles -type directory 
 
Move-Item  -Path  $current_logfiles  -Destination $ $new_logfiles -Recurse -force 

}

请您参考如下方法:

那是因为你有 * ,你告诉它把所有的东西都移到 C:\LogFiles 下.

这应该有效:

$current_logfiles = 'C:\LogFiles' 
$new_logfiles = 'C:\My_Project\LogFiles' 
 
if (!(Test-Path -path $new_logfiles)) { 
  Move-Item -Path $current_logfiles -Destination $new_logfiles -force 
}