首页 > 技术文章 > powershell_移动

moongo 2020-11-30 17:40 原文

Move-Item :移动
copy-Item :复制
作用:从一个位置移动项到另一个位置
详细描述:Move-Item cmdlet 从一个位置移动一项 ( 包括属性 , 内容和子项 ) 到另一个位置 . 这些位置必须由同一提供程
序支持 . 例如 , 它能够从一个目录移动一个文件或子目录到另一个目录 ; 或者从注册表项中移动注册表子项到另一项
中 . 当你移动一项时 , 它被添加到新的位置并从原始位置删除 .
语法:
Move-Item [-path] <string[]> [[-destination] <string>] [-include <string[]>] [-exclude <string[]>]
[-filter <string>] [-force] [-passThru] [-credential <PSCredential>] [-whatIf] [-confirm]
[<CommonParameters>]

Move-Item [-literalPath] <string[]> [[-destination] <string>] [-include <string[]>]
[-exclude <string[]>] [-filter <string>] [-force] [-passThru] [-credential <PSCredential>]
[-whatIf] [-confirm] [<CommonParameters>]

参数:
-path <string[]>
move-item -Path C:\测试\移动\123123.txt D:\测试\移动\
指定项的当前位置路径 . 默认值为当前目录 . 允许使用通配符 .

-destination <string>
Move-Item -Path D:\测试\移动\123123.txt -Destination C:\测试\移动\tao.txt
制定项目被移动到的位置路径 . 默认值为当前目录 . 允许使用通配符 , 但是通配符匹配结果必须是单一的路径 .
要改变移动的项名称 , 在参数 Destination 的值中指定一个新的名称 .

-include <string[]>
移动指定的项 . 此参数值用于限定 Path 参数 . 输入一个路径元素或模式 , 例如 "*.txt" ( 此参数允许通配符 ) .
允许使用通配符 .

-exclude <string[]>
忽略指定的项 . 此参数值用于限定 Path 参数 . 输入一个路径元素或模式 , 例如 "*.txt" ( 此参数允许通配符 ) .
允许使用通配符 .

-filter <string>
指定特定 provider 格式或语言的过滤器 . 此参数值用于限定 Path 参数 . 过滤器的语法取决于 provider ( 是否支
持通配符也依赖 provider) . 过滤器相比其他参数更加有效 , 主要因为 provider 取值时候使用过滤器 , 而不是等到
provider 将所有内容返回后 , 由 Windows PowerShell 过滤对象 .

-force <SwitchParameter>
在不破坏安全性的前提下 , 能够避免影响命令成功执行的限制条件 , 例如 : Force 参数能够覆盖具有只读属性的文件或创
建路径中的必要成分 , 但是不会改变任何文件的权限 .

-passThru <SwitchParameter>
输出此命令创建的对象到管道中 . 默认情况下 , 此命令不会将对象输出到管道 .

-credential <PSCredential>
使用其他凭证进行资源访问认证 . <Credential> 代表着用户名 ( 例如 : "User01" 或 "Domain01/User01") 或者
PSCredential 对象 ( 例如 : 通过 Get-Credential cmdlet 取得的对象 ).

-literalPath <string[]>
指定项的当前位置路径 . 与 Path 不同 , LiteralPath 的值被直接使用 , 不会对任何通配符进行解释 . 如果路径中包含了
转义字符 , 需要将路径用单引号保护 . 单引号指示 Windows PowerShell 不对字符串中的转义字符进行处理 .

-whatIf
描述执行此命令将会发生的现象 , 不会真正执行此命令 .

-confirm
执行命令前提示你进行确认 .


例子1:
C:/PS>move-item -path C:/test.txt -destination E:/Temp/tst.txt
此命令从 C: 驱动器上 , 移动 Test.txt 文件到 E:/Temp 目录并将名称 "test.txt" 改为 " tst.txt".

例子2:
C:/PS>move-item -path C:/Temp -destination C:/Logs
此命令移动目录 C:/Temp 及其所有内容到目录 C:/Logs. 目录 Temp 以及其所有子目录和文件 , 都出现在目录 Logs 中 .

例子3:
C:/PS>move-item -path ./*.txt -destination C:/Logs
此命令移动当前目录 ( 点 (.) 表示当前目录 ) 下所有文本文件 (*.txt) 到目录 C:/Logs.

例子4:
C:/PS>gci . -recurse -include *.txt | move-item -dest C:/ps-test/TextFiles
此命令递归地将当前目录及其子目录下的所有文本文件移动到目录 C:/TextFiles 中 .

例5
C:/PS>move-item hklm:/software/mycompany/* hklm:/software/mynewcompany
此命令将 HKLM/Software 下的 MyCompany 注册表项的注册表项和值移动到 MyNewCompany 项中 . 通配符 (*) 指出
MyCompany 项中的所有内容都需要被移动 , 但是不包括项本身 . 此命令中 , 参数 Path 和 Destination 的名称都可以被省
略 .

例子6:
C:/PS>move-item -literalpath 'Logs[Sept`06]' -destination 'Logs[2006]'
此命令移动 Logs[Sept`06] 目录 ( 及其内容 ) 到目录 Logs[2006] 中 .
使用参数 LiteralPath 代替 Path, 这是因为原始的目录名中包含通配符 [ 和 ]. 路径使用了单引号 (' ') 括起来 , 这样反
引号符号 (`) 就不会被曲解 .

推荐阅读