Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Add User Profiles in SharePoint 2013 using PowerShell Script

, 5 May 2014
Rate this:
Please Sign up or sign in to vote.
Setting up a SharePoint 2013 development environment is essentially a lengthy process. Apart from installation and configuring SharePoint 2013, you need to install SQL Server, Visual Studio etc. to get you started before developing your first SharePoint 2013 App....The post Add User Profiles in Shar

Setting up a SharePoint 2013 development environment is essentially a lengthy process. Apart from installation and configuring SharePoint 2013, you need to install SQL Server, Visual Studio etc. to get you started before developing your first SharePoint 2013 App.

MSDN has a detailed documentation available: Start: Set up the development environment for SharePoint 2013

However if you have Azure development subscription available, you can also use one of the Windows Azure developer image that comes with SharePoint 2013 and Visual Studio 2013 RC pre-installed. The image also contains scripts to provision and configure SharePoint 2013 with or without an Active directory running on a different machine. This is surely a big helping hand as you don’t have to do all the pre-plumbing before you can get started.

Anyway, this is not this blog post about. It was just a background as I was using one of these pre-configured developer images. Once ready, for one of the Apps I was created I wanted to have my User Profile Service to have a considerable number of user profiles. As my development environment was not connected to any domain or active directory, the only users I had access to were the local computer accounts and there were not many. One way was to create the users manually and then import them in user profile, but that does not scale very well and requires repeated effort if I want to set up another similar environment later on. So, I decided to automate the process of creation of local users and then adding them to SharePoint User Profile.

Step 1: Generate the list of accounts to be created

Not really applicable in a real environment where you would have real accounts, but for my development environment I decided to create a Xml file using PowerShell with various elements for a User such as FirstName, LastName, EmailAddress etc.

<User>
    <Account>User0001</Account>
    <EmailAddress>User0001@WORKGROUP.com</EmailAddress>
    <FirstName>FirstName_User1</FirstName>
    <LastName>LastName_User1</LastName>
</User>

Here is the script which generate the specified number of user data in form of an xml file. It takes the number of accounts to generated as input and results into a Users.xml file.

$Path = (Get-Item -Path ".\" -Verbose).FullName + "\Users.xml"
 
$XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)
 
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
 
$xmlWriter.WriteStartDocument()
 
# create root element "Users" and add some attributes to it
$XmlWriter.WriteComment('List of Users')
$xmlWriter.WriteStartElement('Users')

$nrOfUsers = Read-Host 'Please enter the numbers of users you want to create:'
 
# add a couple of random entries
for($x=1; $x -le  [int]$nrOfUsers; $x++)
{
    $User = 'User{0:0000}' -f $x
    $email = $User +  '@' +  (gwmi WIN32_ComputerSystem).Domain
 
    $guid = [System.GUID]::NewGuid().ToString()
 
    # each data set is called "User"
    $xmlWriter.WriteStartElement('User')
   
    # add three pieces of information:
    $xmlWriter.WriteElementString('Account',$User)
    $xmlWriter.WriteElementString('EmailAddress',$email)
    $xmlWriter.WriteElementString('FirstName','User')
    $xmlWriter.WriteElementString('LastName',$x)
 
    # close the "User" node:
    $xmlWriter.WriteEndElement()
}
 
# close the "Users" node:
$xmlWriter.WriteEndElement()
 
# finalize the document:
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

Step 2: Create local account using PowerShell

PowerShell is very useful for automating Active Directory. It allows to quickly and relatively easy automate mundane actions or perform same operations with many objects.

PowerShell provides very broad set of methods to work with Active Directory. There is some of them:

  • Microsoft Active Directory Module for Windows PowerShell
  • ADSI adapter
  • .Net Classes
  • Non Microsoft free extensions, such as Quest Active Directory Cmdlets or AD provider from PowerShell Community Extensions.

Read more about these methods here on MSDN.

$HostName =(gwmi WIN32_ComputerSystem).Name
[ADSI]$server= "WinNT://$HostName" 

$localUsers = $server.Children | where {$_.SchemaClassName -eq 'user'}  |  % {$_.name[0].ToString()}

#Add a new local user after checking that it does not exist
if($localUsers -NotContains $user.Account){
    $Account = $server.Create("User", $user.Account) 
    $password = [System.Web.Security.Membership]::GeneratePassword(10,2)
    $Account.SetPassword($password)
    $Account.SetInfo() 
}
else{
    Write-Host "User Account " $user.Account " already exists"
}

Step 3: Add the User Profile to SharePoint 2013 using PowerShell

You also need to add the user account which is running the PowerShell script to have the Full Control on the User Profile Service application via the Central Administration. To add the permission:

  • Go to Central Administration
  • Go to Manage Service Applications
  • Select User Profile Service Application and click on the Permissions button in the ribbon.
  • Select the user who is running the script and give him ‘Full Control’ access.

User Profile Service Application Permission

You must create the UserProfileManager object before you access a UserProfile object. You can then retrieve the user profiles that the user can access. Full access to everyone’s user profile requires “Manage User Profiles” rights. Full access to your own profile requires “Use Personal Features” rights. Everyone has read access to all profiles.

#instantiate the user profile manager
$mysite = new-object microsoft.sharepoint.spsite($siteurl)
$servicecontext = get-spservicecontext($mysite)
$userprofilemanager = new-object microsoft.office.server.userprofiles.userprofilemanager($servicecontext)

Next, use the userprofilemanager object to check if the profile exists for this particular user. If it does not, the create a new user profile as shown below.

However, this is a crude implementation as I wanted to use it one time. To make it even better, you might also update the existing profile if any with the new data specified in the xml file.

#Add a new user profile for the user
if($userprofilemanager.UserExists($user.Account)){
    Write-Host "User Profile for " $user.Account " already exists"
}
else{
    $newprofile = $userprofilemanager.createuserprofile($user.Account)
    $newprofile.DisplayName = $user.Account
    $newprofile["FirstName"].add($user.FirstName)
    $newprofile["LastName"].add($user.LastName)
    $newprofile["WorkEmail"].add($user.EmailAddress)
    $newprofile.commit()
}    

If you run the CreateUserProfiles.ps1 from the script download, it asks you the following parameters to enter.

  • Number of users to be created. I entered 1000 in this case which seemed to be created without any issues.
  • Site Url of the Central Administration site. This is required to get the context of the service application.

Create User Profile

And the result is as below. Without any manual effort, we have 1000 user profiles ready in our development environment.

User Profiles

Download Scripts

The post Add User Profiles in SharePoint 2013 using PowerShell Script appeared first on Manas Bhardwaj's Stream.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 5 May 2014
Article Copyright 2014 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid