Create SharePoint user profiles using powershell

I have been recently asked to create thousands of user profiles in our test environment.

Ideally importing profiles from AD or provisioning the profiles is advisable, but due to nature of our test environment it was not possible. After searching a bit, I found a script and modified it for this purpose. The script requires all the profile related data to be stored in a CSV.

#Add-PSSnapin if not added already using this link 

$portalsite = "http://spportal"
$portalcontext = Get-SPServiceContext -Site $portalsite


$upm = New-Object -TypeName Microsoft.Office.Server.UserProfiles.UserProfileManager -ArgumentList $portalcontext
$userslist = Import-Csv -Path "userprofilelist.csv"

#Loop through all the rows of csv

for($count=0; $count -lt $userslist.Count; $count++)
{

#Create new User Profile
$upm.CreateUserProfile($userslist[$count].UserName)
$profileproperty = $upm.GetUserProfile($userslist[$count].UserName)

#Custom User Profile being updated for the user
$profileproperty["property1"].Value = $userslist[$count].Property1
$profileproperty["property2"].Value = $userslist[$count].Property2
$profileproperty["property3"].Value = $userslist[$count].Property3
$profileproperty.Commit()

}

Hope this helps. Please see my other scripts to export the data to CSV.

No comments:

Post a Comment