One of my task recently was to migrate to a new DC ‘cuz the old one is totally broken. So I had to export somehow all the users, OUs and SG from the domain controller which was so damaged that I wasn’t able to list the roles installed. That’s why I had to use PowerShell in which I’m newbie. So .. the struggle was real and I decided to share my research and knowledge related to this topic!
As I understood, the best way to export users is CSV file because this is the easiest way to import them directly to the new DC with their old profile configurations.
Get-ADUser -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,
Title,Department,whenCreated,Enabled,Organization | Sort-Object
-Property Name | ConvertTo-CSV
With this command I am telling to PowerShell to export in a CSV file all users from the Active Directory with the following parameters: Name (Rosen Slavchev), SamAccountName (rslavchev or r.slavchev or whatever your sys admin decided to be), Description (“This is test account for our ERP system and ..”), Email (firstname.lastname@example.org), LastLogonDate (the format is: 10:11:12 10.11.12 y), Manager (your manager’ name set in the AD), Title (Systems Engineer), Department (Technical), whenCreated (same format as LastLogonDate), Enabled (will list only ENABLED accounts from the AD), Organization.
After all of this you can open your .csv in Excel and you will be surprised with the good result.
Another command is exporting all Organizational Unites using LDIFDE tool. The command is:
ldifde -f c:\ExportOU.ldf -s domain.local -d “dc=emeneye,dc=co,dc=local” -p subtree -r “(objectcategory=organizationalUnit)” -l “cn,objectclass,ou”
In my case I used local DC but it can be public! The format here is different. I suggest opening with editor like Notepad ++ for best review.
Analog command to this but again in .csv is:
Get-ADOrganizationalUnit -filter * | Select Name, DistinguishedName | export-csv c:\AD_OU_Tree.csv -NoTypeInformation
I hope that this was helpful for PowerShell newbies like me. Do not hesitate to ask me anything regarding this.