Powershell将user\appdata(漫游)文件夹移动到user\appdata\漫游



我正在将文件夹重定向存储移动到新服务器。

然而,appdata的结构现在似乎是users\appdata\Roaming\,而不是旧的结构users\AppData(漫游(\

我正在尝试将一堆用户文件夹的所有文件夹从旧结构移动到新结构。

我假设我需要使用某种循环来检查每个文件夹。

像这样的东西行吗?

$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "P:users$%username%AppDataRoaming"))
{
mkdir ("P:users$%username%AppDataRoaming") | Out-Null
}
Copy-Item P:users$%username%AppData(Roaming)* P:users$%username%AppDataRoaming -recurse -Container
}

我是powershell的新手,所以脚本不是我的最佳选择。

阅读了评论后,这里有一段部分代码,因为我认为您最好使用RoboCopy来完成将所有AppData(Roaming)文件夹复制到新AppDataRoaming文件夹的繁重工作。

请先在一个虚拟用户身上进行测试,找出在robocopy上使用什么开关。然后当然还要测试新路径上的用户权限是否正确(从P:Users$<username>文件夹继承(

$userShare = 'P:Users$'      # if running on the server, otherwise best use the UNC path
# loop through tyhe folders inside the user share (1st level only)
Get-ChildItem -Path $userShare -Directory | ForEach-Object {
Write-Host "Processing user $($_.Name)"
$destinationDir = Join-Path $_.FullName -ChildPath ('AppDataRoaming')
if (!(Test-Path -Path $destinationDir -PathType Container)) {
Write-Host "Creating folder '$destinationDir'"
$null = New-Item -Path $destinationDir -ItemType Directory
}
$wrongRoamingDir = Join-Path $_.FullName -ChildPath ('AppData(Roaming)')
if (Test-Path -Path $wrongRoamingDir -PathType Container) {
#############################################################################################
# here is where you start copying everything from the $wrongRoamingDir to the $destinationDir.
# I would suggest using RoboCopy for that using switches like
# Robocopy /MIR $wrongRoamingDir  $destinationDir
# or
# Robocopy /S /E $wrongRoamingDir  $destinationDir
#
# Please test on a dummy user first. For more robocopy switches, have a look at
# https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
# https://www.windows-commandline.com/robocopy-command-syntax-examples/
#############################################################################################
}
}

最新更新