Saturday, December 20, 2014

How to add users in .txt file to SharePoint Groups In using PowerShell?

Power Shell


# Adding the PowerShell Snapin
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
# Get the SiteURL
$webUrl = "http://mysite/"
$web = Get-SPWeb $webUrl
$fileWithPath = "D:\user.txt"
$content = Get-Content $fileWithPath
$spGroup = "Security Read"
$rd="Read"



foreach($user in $content)
{    
    try
    {
        $spUser = $web.EnsureUser($user)
        if($spUser -ne $null)
        {
        Write-Host $spuser
            Write-Host "Adding " $spUser
            $web.SiteGroups[$spGroup].AddUser($spUser)
            Write-Host $user  " User added to the
group successfully" -foregroundcolor Green } else { Write-Host $spUser " does not exists" } } catch { Write-Host "Error occured on " $user -foregroundcolor Red } }


No comments:

Post a Comment