Get All AD users details using PowerShell and Export results as CSV

Today i have faced one scenario to export all the users details to Excel/CSV thru PowerShell.

Use the below script for the above scenaio


Add-PSSnapin "Microsoft.SharePoint.PowerShell"
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")

$userProfiles = @()
$mysiteHostUrl = "http://SitteURL/my"

$mysite = Get-SPSite $mysiteHostUrl
$context = [Microsoft.Office.Server.ServerContext]::GetContext($mysite)
$upm =  New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

$AllProfiles = $upm.GetEnumerator()

foreach($profile in $AllProfiles)
{
    write-host $profile["AccountName"].Value

    $profileObject = New-Object PSObject

    $profileObject | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $profile.DisplayName
    $profileObject | Add-Member -MemberType NoteProperty -Name "Account Name" -Value $profile["AccountName"].Value
    $profileObject | Add-Member -MemberType NoteProperty -Name "Email" -Value $profile["WorkEmail"].Value

    $userProfiles += $profileObject
    #write-host $profile.DisplayName + '-' + $profile["AccountName"].Value + '-' + $profile["WorkEmail"].Value
}

$mysite.Dispose();

$userProfiles | Export-Csv -LiteralPath "c:\UsersInfo.csv" -NoTypeInformation 

Comments

Popular posts from this blog

This content database has a schema version which is not supported in this farm

Export all documents/Folders permissions to CSV using SharePoint PowerShell

SPUtility.js