How to Export users from ADSI in Server 2003

Migrated from old website (first published Oct 13, 2017)

Recently, with a restricted Server 2003 domain controller with PowerShell 1.0, I had to export the users to import them into another Server 2003 (I know, normally I would say DON’T DO IT! but it is a special system that has no external connection and the software only works with level 2003).

Since PowerShell 1.0 has not given the command Get-ADUser and an installation of additional tools was not possible, I have carried out the export of the users via ADSI using a PowerShell script and saved the user data in a CSV, which can be imported on the other server.

That you don’t have to search and tinker, you can copy the content below and paste in a notepad, then save it as Get-UserExportADSI.ps1.

<#

.SYNOPSIS
 Collects users and attributes of this users and write/export it to a CSV file.

 Adrian Dolder - DOUDISBLOG

 THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
 RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.

 Version 1.0, 2016-03-16

 Ideas, comments and suggestions to script.feedback@doudi.ch
 Please give the information about the Scriptname, Version and the Developer.

.LINK 
 -

.DESCRIPTION

 This script collects and exports from ADSI the users of a specified OrganizationalUnit
 and takes the attributes, then write all the information to a CSV file.
 
 The script exports/writes the information to a CSV file unter C:\temp named userexport.csv
 
 The search scope is set to the subtree.

 If you want to set the search scope to OneLevel, you need to set a # in front of the line
 $objSearch.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
 
 and delete the # on the line
 #$objSearch.SearchScope = [System.DirectoryServices.SearchScope]::OneLevel
 
.NOTES
 Requirements
 - Windows Server 2003
 - PowerShell 1.0
 - Domain Admin rights

 Revision History
 --------------------------------------------------------------------------------
 1.0     Initial community release

.PARAMETER
 -

.EXAMPLE
 .\Get-UserExportADSI.ps1

  #>


# Define the path and file for the export
$csvPath = 'C:\temp\userexport.csv'

# Define the OrganizationalUnit (subtree will be searched, can be changed to OneLevel)
$OU = 'ou=OrganizationUnit,dc=Domain,dc=local'
$objSearch = New-Object System.DirectoryServices.DirectorySearcher 
$objSearch.PageSize = 15000 
$objSearch.Filter = "(&(objectCategory=User)(objectCategory=Person))"
$objSearch.SearchRoot = "LDAP://$OU"
$objSearch.SearchScope = [System.DirectoryServices.SearchScope]::Subtree

#$objSearch.SearchScope = [System.DirectoryServices.SearchScope]::OneLevel

# Definition of what will be exported
$allUsers = $objSearch.FindAll() 

$all = @()
foreach ($user in $allUsers) {
    $o = $user.GetDirectoryEntry()

    $propertyHT = @"
        Username = $($o.SamAccountName.toString())
        Vorname = $($o.Givenname.ToString())
        Nachname = $($o.sn.toString())
        Initialien = $($o.initials.toString())
        Rufnummer = $($o.telephoneNumber.toString())
        E-Mail = $($o.mail.toString())
        Webseite = $($o.wWWHomePage.toString())
        Straße = $($o.streetAddress.toString())
        Ort = $($o.l.toString())
        PLZ = $($o.postalCode.toString())
        Nummer Privat = $($o.HomePhone.toString())
        Nummer Pager = $($o.pager.toString())
        Nummer Fax = $($o.facsimileTelephonenumber.toString())
        Position = $($o.Title.toString())
        Abteilung = $($o.Department.toString())
        Firma = $($o.Company.toString())
        Vorgesetzter = $(if($o.Manager -ne $null){$o.Manager.toString().Split(",")[0].Substring(3)})
        Mitarbeiter= $o.directReports.toString()
"@

 $all += new-Object PSObject -Property (ConvertFrom-StringData -StringData $propertyHT)

 }
 

# write the CSV file
$all | export-csv $csvPath -Delimiter ";" -NoTypeInformation -Encoding UTF8 -Force

Leave a comment

Blog at WordPress.com.

Up ↑