首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell将user\appdata(漫游)文件夹移动到user\appdata\roaming

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

Stack Overflow用户
提问于 2021-03-13 19:09:58
回答 1查看 264关注 0票数 0

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

但是,appdata的结构现在似乎是users\AppData\Roaming\,而不是旧结构users\AppData(Roaming)\

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

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

像这样的东西可以工作吗?

代码语言:javascript
复制
$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "P:\users$\%username%\AppData\Roaming"))
{
    mkdir ("P:\users$\%username%\AppData\Roaming") | Out-Null
}
Copy-Item P:\users$\%username%\AppData(Roaming)\* P:\users$\%username%\AppData\Roaming\ -recurse -Container
}

我是powershell的新手,所以编写脚本不是我的拿手好戏。

EN

回答 1

Stack Overflow用户

发布于 2021-03-13 23:46:17

在阅读了注释之后,这里是一段部分代码,因为我认为您最好使用RoboCopy来做繁重的工作,将所有内容从AppData(Roaming)文件夹复制到新的AppData\Roaming文件夹。

请先在虚拟用户上测试,以确定在robocopy上使用哪些开关。当然,还要测试新路径上的用户权限是否正确(从P:\Users$\<username>文件夹继承)

代码语言:javascript
复制
$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 ('AppData\Roaming')
    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://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
        # https://www.windows-commandline.com/robocopy-command-syntax-examples/
        #############################################################################################
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66612747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档