网站搜索

使用 PowerShell 将 AD 用户导出到 CSV


我们希望使用 PowerShell 将 AD 用户信息导出到 CSV。为什么我们需要使用PowerShell列出AD用户?例如,我们想知道每个 AD 用户在 Active Directory 中是否都有正确的手机号码。这是因为服务台在拨打电话之前会在 Active Directory 中查找信息。在本文中,您将了解如何使用 PowerShell 将 Active Directory 用户导出到 CSV 文件。

导出AD用户信息PowerShell脚本

Export-ADUsers.ps1 PowerShell 脚本将针对您设置的 distinguishedName 运行。之后,它将报告导出为 CSV 文件。您可以使用 Microsoft Excel 或支持 CSV 文件扩展名的任何其他应用程序打开 CSV 文件。

该脚本将收集每个用户的以下信息:

  1. 名

  2. 姓

  3. 显示名称

  4. 用户登录名

  5. 用户主体名称

  6. 街道

  7. 城市

  8. 州/省

  9. 邮编/邮政编码

  10. 国家/地区

  11. 职称

  12. 部门

  13. 公司

  14. 经理

  15. 欧

  16. 描述

  17. 办公室

  18. 电话号码

  19. 其他电话

  20. 电子邮件

  21. 移动的

  22. 寻呼机

  23. 笔记

  24. 帐户状态

  25. 用户创建日期

  26. 上次登录日期

使用 PowerShell 将 Active Directory 用户导出到 CSV

让我们完成这些步骤并使用 PowerShell 将 Active Directory 用户导出到 CSV 文件。

步骤 1:准备导出 AD 用户 PowerShell 脚本

下载 Export-ADUsers.ps1 PowerShell 脚本并将其放置在域控制器 C:\scripts 文件夹中。如果您没有脚本文件夹,请创建一个。

确保该文件未被阻止,以防止运行脚本时出现任何错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Export-ADUsers.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Export-ADUsers.ps1

    .DESCRIPTION
    Export Active Directory users to CSV file.

    .LINK
    alitajran.com/export-ad-users-to-csv-powershell

    .NOTES
    Written by: ALI TAJRAN
    Website:    alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 05/24/2021 - Initial version
    V1.10, 04/01/2023 - Added progress bar, user created date, and OU info
    V1.20, 05/19/2023 - Added function for OU path extraction
#>

# Split path
$Path = Split-Path -Parent "C:\scripts\*.*"

# Create variable for the date stamp in log file
$LogDate = Get-Date -f yyyyMMddhhmm

# Define CSV and log file location variables
# They have to be on the same location as the script
$Csvfile = $Path + "\AllADUsers_$LogDate.csv"

# Import Active Directory module
Import-Module ActiveDirectory

# Function to extract OU from DistinguishedName
function Get-OUFromDistinguishedName {
    param(
        [string]$DistinguishedName
    )

    $ouf = ($DistinguishedName -split ',', 2)[1]
    if (-not ($ouf.StartsWith('OU') -or $ouf.StartsWith('CN'))) {
        $ou = ($ouf -split ',', 2)[1]
    }
    else {
        $ou = $ouf
    }
    return $ou
}

# Set distinguishedName as searchbase, you can use one OU or multiple OUs
# Or use the root domain like DC=exoip,DC=local
$DNs = @(
    "OU=Sales,OU=Users,OU=Company,DC=exoip,DC=local",
    "OU=IT,OU=Users,OU=Company,DC=exoip,DC=local",
    "OU=Finance,OU=Users,OU=Company,DC=exoip,DC=local"
)

# Create empty array
$AllADUsers = @()

# Loop through every DN
foreach ($DN in $DNs) {
    $Users = Get-ADUser -SearchBase $DN -Filter * -Properties * 

    # Add users to array
    $AllADUsers += $Users

    # Display progress bar
    $progressCount = 0
    for ($i = 0; $i -lt $AllADUsers.Count; $i++) {

        Write-Progress `
            -Id 0 `
            -Activity "Retrieving User " `
            -Status "$progressCount of $($AllADUsers.Count)" `
            -PercentComplete (($progressCount / $AllADUsers.Count) * 100)

        $progressCount++
    }      
}

# Create list
$AllADUsers | Sort-Object Name | Select-Object `
@{Label = "First name"; Expression = { $_.GivenName } },
@{Label = "Last name"; Expression = { $_.Surname } },
@{Label = "Display name"; Expression = { $_.DisplayName } },
@{Label = "User logon name"; Expression = { $_.SamAccountName } },
@{Label = "User principal name"; Expression = { $_.UserPrincipalName } },
@{Label = "Street"; Expression = { $_.StreetAddress } },
@{Label = "City"; Expression = { $_.City } },
@{Label = "State/province"; Expression = { $_.State } },
@{Label = "Zip/Postal Code"; Expression = { $_.PostalCode } },
@{Label = "Country/region"; Expression = { $_.Country } },
@{Label = "Job Title"; Expression = { $_.Title } },
@{Label = "Department"; Expression = { $_.Department } },
@{Label = "Company"; Expression = { $_.Company } },
@{Label = "Manager"; Expression = { (Get-AdUser $_.Manager -Properties DisplayName).DisplayName } },
@{Label = "OU"; Expression = { Get-OUFromDistinguishedName $_.DistinguishedName } },
@{Label = "Description"; Expression = { $_.Description } },
@{Label = "Office"; Expression = { $_.Office } },
@{Label = "Telephone number"; Expression = { $_.telephoneNumber } },
@{Label = "Other Telephone"; Expression = { $_.otherTelephone -join ";"} },
@{Label = "E-mail"; Expression = { $_.Mail } },
@{Label = "Mobile"; Expression = { $_.mobile } },
@{Label = "Pager"; Expression = { $_.pager } },
@{Label = "Notes"; Expression = { $_.info } },
@{Label = "Account status"; Expression = { if (($_.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
@{Label = "User created date"; Expression = { $_.WhenCreated } },
@{Label = "Last logon date"; Expression = { $_.lastlogondate } } |

# Export report to CSV file
Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"
  • 第54,55,56行:编辑目标distinguishedName。您可以拥有一个 OU 或多个 OU(在此示例中)。

第 2 步:获取专有名称

您需要在 PowerShell 脚本中添加可分辨名称值。请按照以下步骤获取 Active Directory 中的可分辨名称:

  1. 启动Active Directory 用户和计算机

  2. 右键单击目标 OU,然后单击属性

  3. 转到属性编辑器选项卡

  4. 在属性列表中查找属性 distuingedName

  5. 双击打开字符串并复制值

注意:如果您没有看到属性编辑器选项卡,请单击查看菜单栏中的“Active Directory 用户和计算机”,然后启用高级功能。

步骤 3:运行导出 AD 用户 PowerShell 脚本

以管理员身份运行 PowerShell。更改 scripts 文件夹的路径。运行 PowerShell 脚本将 AD 用户导出到 CSV 文件。等到它完成。

PS C:\> cd c:\scripts
PS C:\scripts> .\Export-ADUsers.ps1

步骤 4:打开 AD 用户报告 CSV 文件

转到脚本文件夹并验证您是否看到 AllADUsers_ 文件。

使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。

一切看起来都很棒!

了解更多:使用 PowerShell 从组中删除用户 »

结论

您了解了如何使用 PowerShell 将 AD 用户导出到 CSV。每个用户帐户中都包含大量信息。使用 PowerShell,您可以获得适合您需求的自定义报告。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 获取组织单位。不要忘记关注我们并分享这篇文章。

相关文章