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

Powershell 移动包含异常文件的文件夹,powershell,exception,move,Powershell,Exception,Move,我正在尝试从“文件”文件夹中移动数据,并排除用户文件夹。但最终,这个例外并不起作用 Move-Item -Path $env:SystemDrive\$env:computername\File\* -exclude $env:SystemDrive\$env:computername\File\Users\* -Destination $env:SystemDrive\UserOld\ 必须传输数据并排除用户文件夹。我想谈的第一件事是如何创建路径。您不应该按现在的方式创建它们,而应该使用以

我正在尝试从“文件”文件夹中移动数据,并排除
用户
文件夹。但最终,这个例外并不起作用

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

必须传输数据并排除
用户
文件夹。

我想谈的第一件事是如何创建路径。您不应该按现在的方式创建它们,而应该使用以下命令:)

这就是我如何处理您当前的问题:

#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 -Path C:\Users\D.Baier\Desktop\testenvironment\Source -Exclude mit-1 -Destination C:\Users\D.Baier\Desktop\testenvironment\Target\
它似乎工作得很完美,只是抛出了一个错误,至少据我所知,似乎是这样。 顺便说一句,错误如下:

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

因此,这是一个纯英语的网站,请更新您的答案,以英语为您提供的错误消息目前是不可读:)完成-希望澄清的事情:)