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

如何使用PowerShell查询AD将姓名命名的JPG文件批量重命名为用户名格式

需求说明

现有一批JPG格式的员工照片文件,文件名按「姓氏, 名字」规则命名,示例为Smith, John.jpg。需要将文件复制后按对应用户的用户名重命名,目标文件名示例为jsmith.jpg。

实现思路
  • 从原文件名中拆分提取姓氏和名字字段
  • 以提取的姓名字段为条件查询AD域,获取对应用户的SAMAccountName属性
  • 将原文件复制后,以查询得到的SAMAccountName作为新文件名存储
最终实现脚本(PowerShell)
Import-Module ActiveDirectory
$Folder = "G:\Photos" # 存储JPG文件的文件夹路径
$Files = Get-ChildItem $Folder

foreach ($file in $files)
{
    $CurrentFileName = $file.Name
    $SplitCurrent = $CurrentFileName.Split(", ")
    $LastName = $SplitCurrent[0]
    $FirstName = $SplitCurrent[2].Substring(0, $SplitCurrent[2].Length - 4)
    $UserName = Get-ADUser -Filter 'GivenName -eq $FirstName -and sn -eq $LastName' -Properties SamAccountName | Select-Object -ExpandProperty SamAccountName
    $NewFileName = $UserName + ".jpg"

    Copy-Item -Path $file.FullName -Destination "$Folder\$NewFileName"
}

内容的提问来源于stack exchange,提问作者phaelium

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 13:48:03