You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell批量移动重定向用户文件夹文件到Documents子目录

文件夹重定向目录批量整理PowerShell方案

适配场景:文件服务器FolderRedirection目录下,为每个用户根文件夹新建Documents子目录,将用户根目录下散落的原有文件统一移入该子目录,匹配如下结构转换:

  • 原有结构
―FolderRedirection
―――JContoso
――――――File.docx
――――――File.pptx
―――MDerby
――――――File.docx
――――――File.pptx
  • 目标结构
―FolderRedirection
―――JContoso
――――――Documents
―――――――――File.docx
―――――――――File.pptx
―――MDerby
――――――Documents
―――――――――File.docx
―――――――――File.pptx

使用注意事项

  • 执行前将脚本中$rootPath变量值替换为你服务器上FolderRedirection目录的实际绝对路径
  • 首次执行建议先复制2个测试用户目录到临时路径跑脚本验证逻辑,确认符合预期后再对正式目录全量执行
  • 脚本默认仅移动用户根目录下直接存放的文件,不会改动原有其他子目录内容
  • 遇到目标路径下同名文件时会自动给文件追加序号,不会强制覆盖原有文件
  • 移动操作会保留文件原有的创建/修改时间、权限属性

完整脚本

# 替换为你的文件夹重定向根目录实际路径
$rootPath = "C:\Share\FolderRedirection"

# 枚举根目录下所有一级用户文件夹
$userFolderList = Get-ChildItem -Path $rootPath -Directory
foreach ($userFolder in $userFolderList) {
    # 拼接当前用户的Documents目录路径
    $docFolderPath = Join-Path -Path $userFolder.FullName -ChildPath "Documents"
    # Documents目录不存在则自动创建
    if (-not (Test-Path -Path $docFolderPath -PathType Container)) {
        New-Item -Path $docFolderPath -ItemType Directory -Force | Out-Null
    }

    # 枚举当前用户根目录下直接存放的所有文件
    $filesToMove = Get-ChildItem -Path $userFolder.FullName -File
    foreach ($file in $filesToMove) {
        $targetFilePath = Join-Path -Path $docFolderPath -ChildPath $file.Name
        # 同名文件自动加序号避免覆盖
        $dupCount = 1
        while (Test-Path -Path $targetFilePath) {
            $targetFilePath = Join-Path -Path $docFolderPath -ChildPath (
                "{0}_{1}{2}" -f $file.BaseName, $dupCount, $file.Extension
            )
            $dupCount++
        }
        # 执行移动
        Move-Item -Path $file.FullName -Destination $targetFilePath -Force
    }
    Write-Host "处理完成:$($userFolder.Name)"
}
Write-Host "全部用户目录处理完毕"

自定义调整

如果需要把用户根目录下除新建的Documents外的其他原有子文件夹也一并移入Documents,把脚本中$filesToMove = Get-ChildItem -Path $userFolder.FullName -File这行替换为以下代码即可:

$filesToMove = Get-ChildItem -Path $userFolder.FullName | Where-Object { $_.Name -ne "Documents" }

内容的提问来源于stack exchange,提问作者Pejmon H.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 22:24:24