Powershell 获取当前用户samaccountname

# 获取当前用户的对象
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()

# 提取用户名(去掉域前缀)
$userName = $currentUser.Name.Split('\')[-1]

# 获取当前用户的 sAMAccountName
$samAccountName = (Get-ADUser -Identity $userName -Properties sAMAccountName).sAMAccountName

# 输出 sAMAccountName
Write-Host "Current user's sAMAccountName: $samAccountName"

可以通过下面的命令查看用户的SAMAccountName是否存在于AD中:

Get-ADUser -Identity "Name" -Properties sAMAccountName