#PowerShell 的 Move-Item 命令
Move-Item
[-Path] <String[]>
[[-Destination] <String>]
[-Force]
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-PassThru]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
功能
将项及其属性、内容和子项从一个位置移动到另一个位置。
类型
Cmdlet,属于 Microsoft.PowerShell.Management。
参数
[-Path] <String[]>- 要移动的项路径;支持通配符-LiteralPath <String[]>- 按字面值解释路径,不将任何字符视为通配符[-Destination] <String>- 目标位置,默认为当前目录;可以同时指定新名称-Force- 覆盖目标位置已存在的项-Filter <String>- 由提供程序执行的筛选器,通常比-Include更高效-Include <String[]>- 只包含匹配的项;仅对-Path的通配符匹配结果生效-Exclude <String[]>- 排除匹配的项;仅对-Path的通配符匹配结果生效-PassThru- 返回表示被移动项的对象;默认不产生输出-WhatIf- 显示将要执行的操作而不实际执行;别名wi-Confirm- 执行命令前提示确认;别名cf
完整参数可使用
Get-Help Move-Item -Full查看。
移动操作会删除原位置的项。移动目录时,其所有子项都会一并移动,因此移动本质上是递归的。
#示例
移动文件
> Move-Item -Path ./draft.md -Destination ./docs/
- 文件会从原位置删除,并出现在
./docs/中。
移动并重命名
> Move-Item -Path ./report.txt -Destination ./archive/report-2026.txt
- 在
-Destination中指定新文件名即可同时完成移动和重命名。
移动所有匹配的文件
> Move-Item -Path ./logs/*.log -Destination ./archive
-Path支持通配符,可以一次移动多个文件。
递归移动特定文件
> Get-ChildItem -Path ./src -Filter *.ps1 -Recurse | Move-Item -Destination ./scripts
- 管道会把
Get-ChildItem找到的每个文件传递给Move-Item。 - 目标目录不存在时命令会失败,
-Force也不会自动创建目录。
预览操作
> Move-Item -Path ./old -Destination ./archive -WhatIf
What if: Performing the operation "Move Directory" on target "Item: /home/user/old Destination: /home/user/archive/old".
-WhatIf只显示将要执行的操作,不会真正移动任何内容。