Monday, January 20, 2014

How to Use RunWithElevatedPrivileges in PowerShell Scripts for SharePoint

We use RunWithElevatedPrivileges method to impersonate System Account (Application pool identity), which is granted with FULL control access rights via web application user policy. Here is an example of using RunwithElevatedPrivileges with PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{

    #Define the parameter values: Site collection URL and user account to remove
    $siteURL =  "http://sharepoint.crescent.com/sites/sales"
    $userAccount = "crescent\Salaudeen" 
  
    #Get the RootWeb
    $web= Get-SPWeb $siteURL
    #Get the user acount - If doesn't exists ADD
    $user = $web.EnsureUser($userAccount)
 
    #Make the user as Site collection Admin
    $user.IsSiteAdmin = $true
    $user.Update()
 
    #Print a message
    Write-host "User: $($userAccount) has been added as site collection administrator!"   
 
 }
)  
This PowerShell example uses run with elevated privileges in SharePoint to add new site collection administrator.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...