不使用Out-GridView,用WinForms消息框显示AD用户SamAccountName
解决方案
- 先加载Windows Forms程序集(PowerShell默认不会自动加载):
Add-Type -AssemblyName System.Windows.Forms
- 获取目标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"
- 调用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
相关产品推荐
相关产品推荐

