我正在尝试从“文件”文件夹移动数据,并排除Users
文件夹。但最后,该异常不起作用。
Move-Item -Path $env:SystemDrive\$env:computername\File\* -exclude $env:SystemDrive\$env:computername\File\Users\* -Destination $env:SystemDrive\UserOld\
有必要传输数据并排除Users
文件夹。
答案 0 :(得分:0)
我要谈的第一件事是如何创建路径。您不应该按照自己的方式创建它们,而应该使用Join-Path
命令:)
这是我可能会解决您当前问题的方法:
#Creating path to source folder
$source = Join-Path -Path "$env:SystemDrive\$env:COMPUTERNAME" -ChildPath "File"
#Creating path to destination folder
$destination = Join-Path -Path $env:SystemDrive -ChildPath "UserOld"
#Looping through all the folders in the source
Get-ChildItem -Path $source -Directory -Recurse | ForEach-Object{
#Only moving the folder if the name isn't "Users"
if ($_.Name -ne "Users"){
Write-Host "Currently Moving: $_.Name"
#Copy-Item -Path $_.FullName -Destination $destination
}
}
让我知道您的工作状况,我已将实际的活动部分注释掉,因为我鼓励您进行更多的测试:)
答案 1 :(得分:0)
我尝试使用move-item
来移动文件夹,同时排除单个文件夹,看来您不需要在排除中包括整个路径。
我尝试过:
Move-Item -Path C:\Users\D.Baier\Desktop\testenvironment\Source -Exclude mit-1 -Destination C:\Users\D.Baier\Desktop\testenvironment\Target\
它似乎运行良好,只是抛出了一个错误,似乎是a known issue,至少据我所知。 错误是以下顺便说一句:
Move-Item : The Element cannot be moved, since the Element, located at "C:\Users\D.Baier\Desktop\testenvironment\Source\mit-1" does not exist.
In Line:1 Charakter:1
+ Move-Item -Path C:\Users\D.Baier\Desktop\testenvironment\Source \* -Exclu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
希望有帮助!
编辑:对不起,我的电脑设置为德语。我尽我所能翻译了错误消息,但是我怀疑如果您在英语计算机上运行此代码,是否会得到与该错误消息完全相同的信息。对于可能出现的拼写错误,我也深表歉意。
答案 2 :(得分:0)
粗心
#Creating path to source folder
$source = Join-Path -Path "$env:SystemDrive\$env:COMPUTERNAME" -ChildPath "\File\C$\"
#Creating path to destination folder
$destination = Join-Path -Path $env:SystemDrive -ChildPath "UserOld"
$h = "Users", "Windows", "ProgramData"
#Looping through all the folders in the source
Get-ChildItem -Path $source -Recurse | ForEach-Object{
#Only moving the folder if the name isn't "Users"
if ($_.Name -ne "$h"){
Write-Host "Currently Moving: $_.Name"
}
Move-Item -Path $_.FullName -Exclude $h -Destination $destination
}
此选项移动不带内容的Users文件夹。 因此,合并的答案。
#Creating path to destination folder
$destination = Join-Path -Path $env:SystemDrive -ChildPath "UserOld"
#Move with the exception
Move-Item -Path $env:SystemDrive\$env:computername\USMT\File\C$\* -Exclude "Users", "Windows","ProgramData","Program Files","Program Files (x86)" -Destination $destination