0
votes

I have a script running every month that runs through all of my Users OU's and creates a text file back up of all the users within each OU. The properties exported for each user are:

name, DisplayName, SamAccountName, EmailAddress, Title, Description, Department, Division, Manager, ScriptPath, TelephoneNumber, mobilephone, employeetype, StreetAddress, city, Country, co, company, memberof

Just a quick background, there have been a few instances where the wrong user was deleted and the new account needed to be recreated on the fly. At least with this, there will be some record of each account that I can quickly use to recreate a user that was accidently deleted. My question is this, how can I use the data from the out-file and create a new-aduser with the information correlating to each property?

1
Why do you delete accounts? Disable them and if necessary, move them to another OU. Delete them only after being disabled for x months (or never). One thing is to recreate their info, but group memberships are gone, SID is gone etc.... Or raise to 2008 R2 forest level so you can use Recycle Bin to restore the deleted account... If you still want the script, search for New-ADUser, Set-ADUser and Add-ADGroupMember - Frode F.
Thanks for the quick response! I don't intentionally delete them but there have been times in the past where another engineer by accident deletes an account or after 90 days, a person may be rehired and that info may be gone. - user6015403
That's why I recommend to never delete. You can even block it so people can only disable and move. - Frode F.

1 Answers

0
votes

I believe you can use below code snippet after splitting the properties from your exported text file, once for each user in a foreach loop:

    # Getting the manager
    $manager = Get-ADUser -Identity <managerName>

    # Creating New AD User
    $newUser = New-ADUser -Name <string> -DisplayName <string> -SamAccountName <string> -EmailAddress <string> -Title <string> -Description <string> -Department <string> -Division <string> -Manager $manager -ScriptPath <string> -OfficePhone <string> -MobilePhone <string> -Type <string> -StreetAddress <string> -City <string> -Country <string> -Company <string> 

    # Fetching and Adding the User to the Group
    $group = Get-ADGroup "CN=AccountLeads,OU=UserAccounts,DC=ABC,DC=FABRIKAM,DC=COM" –Server "abc.fabrikam.com";
    Add-ADGroupMember $group –Member $newUser –Server "abc.fabrikam.com"

You can split and loop on various groups from your "memberOf" property and repeat the last two lines for each group.

References: