移动包含异常文件的文件夹

Moving folders with the files with exceptions

我正在尝试从 "file" 文件夹中移动数据并排除 Users 文件夹。但最后,异常不起作用。

Move-Item -Path $env:SystemDrive$env:computername\File\* -exclude $env:SystemDrive$env:computername\File\Users\*  -Destination $env:SystemDrive\UserOld\

需要传输数据并排除Users文件夹。

我想谈的第一件事是您如何创建路径。您不应该按照自己的方式创建它们,而应该使用 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
    }

}

让我知道您的进展情况,我已将实际移动部分注释掉,因为我鼓励您进行更多测试:)

我尝试使用 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

希望对您有所帮助!

编辑:抱歉,我的电脑设置为德语。我尽可能地翻译了错误消息,但我怀疑如果您在英语机器上 运行 这段代码,它是否与您得到的完全相同。对于我可能犯的任何拼写错误,我也深表歉意。

大意

#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
}

此选项移动没有内容的用户文件夹。 为此,综合回答。

#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