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

不使用Out-GridView,用WinForms消息框显示AD用户SamAccountName

解决方案
  1. 先加载Windows Forms程序集(PowerShell默认不会自动加载):
Add-Type -AssemblyName System.Windows.Forms
  1. 获取目标OU内所有用户的SamAccountName,并拼接成每行一个的字符串:
# 替换为你的OU路径,示例格式:"OU=Users,DC=contoso,DC=com"
$ouPath = "OU=你的目标OU,DC=域名,DC=后缀"
$userList = Get-ADUser -Filter * -SearchBase $ouPath | Select-Object -ExpandProperty SamAccountName
$displayText = $userList -join "`n"
  1. 调用MessageBox展示内容:
[System.Windows.Forms.MessageBox]::Show($displayText, "OU内用户列表", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)

补充说明

如果OU内用户数量较多,MessageBox可能因内容长度限制显示不全,可改用带滚动条的TextBox弹窗:

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$form.Text = "OU内用户列表"
$form.Size = New-Object System.Drawing.Size(400, 600)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Multiline = $true
$textBox.ScrollBars = "Vertical"
$textBox.Dock = "Fill"
$textBox.Text = $displayText

$form.Controls.Add($textBox)
$form.ShowDialog() | Out-Null

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:17:05