How To: Add a User in a SharePoint Site Using PowerShell

How To: Add a User in a SharePoint Site Using PowerShell


 This topic is a how to.
Please keep it as clear and simple as possible. Avoid speculative discussions as well as a deep dive into underlying mechanisms or related technologies.


What is Windows Powershell ?


Windows PowerShell command-line interface is a new command-line tool and supporting scripting language from Microsoft that complements Cmd.exe in the Windows administration context. In the SharePoint administration context, Windows PowerShell supersedes the Stsadm.exe administration tool. Moving forward, you should use Windows PowerShell scripting technology to develop any new command-line scripts in SharePoint Foundation 2010.

Firstly we need to load sharepoit assembly in powershell console which is the reference to all sharepoint objects.

PS C:\Users\sunil> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")

GAC Version Location --- ------- -------- True v2.0.50727 C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint\12.0.0.0__71e9bce111e9429c\Microsoft.SharePo...

Create a site object to hold the SPSite reference.

PS C:\Users\sunil> $site=new-object Microsoft.Sharepoint.SPSite("http://mossportal")

obtain an reference to the Web site that is associated with the URL that is used in an SPSite constructor.

PS C:\Users\sunil> $web=$site.OpenWeb()

Now defines the role assignments for a user.

PS C:\Users\sunil> $spRoleAssignment=new object(SPRoleAssignment("mossportal\sunil", "sunilyadav165@gmail.com", "Sunil Yadav", "Sharepoint developer")

Get the role from the web and set the base permission for a role definition.

>> $spRoleDefinition=$web.RoleDefinitions["Read"]

>> $spRoleAssignment.RoleDefinitionBindings.Add($spRoleDefinition)

Finally add the RoleAssignments to the current we and update the spweb object.

>> $web.RoleAssignments.Add($spRoleAssignment)

>> $web.Update()


See Also


Other Languages

This articles is also available in the following languages:

Italian (it-IT)

French (fr-FR)

 
Leave a Comment
  • Please add 4 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Luigi Bruno edited Revision 5. Comment: Added the "How To" tag and Wiki Message Template. Added the "See also" and "Other Languages" sections.

  • Richard Mueller edited Revision 4. Comment: Replaced RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 3. Comment: Removed (en-US) from title, added tag

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

Page 1 of 1 (4 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Benoit Jester edited Revision 6. Comment: Add link to french article

  • Luigi Bruno edited Revision 5. Comment: Added the "How To" tag and Wiki Message Template. Added the "See also" and "Other Languages" sections.

  • Richard Mueller edited Revision 4. Comment: Replaced RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 3. Comment: Removed (en-US) from title, added tag

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

Page 1 of 1 (5 items)