Home > SharePoint 2010 > PowerShell Import all AD users to SharePoint 2010 Group

PowerShell Import all AD users to SharePoint 2010 Group

$spgroupname = "Group Name"
$sitename= "http://sitename"
$domain = $env:USERDOMAIN

$spsite = SPSite($sitename)
$rootweb = $spsite.rootweb
$spgroup = $rootweb.Groups[$spgroupname]

$allowUnsafeUpdates = $spsite.AllowUnsafeUpdates
$spsite.allowUnsafeUpdates = 1

$searcher = New-Object 
System.DirectoryServices.DirectorySearcher("(objectCategory=User)")
$results = $searcher.FindAll()
foreach($result in $results){
	$user=$result.GetDirectoryEntry()
	$username = $domain + "\" + $user.sAMAccountName
	$spsiteuser = $rootweb.EnsureUser($username)
        $spUser = $rootweb.AllUsers[$spsiteuser]
        $spgroup.AddUser($spsiteuser)
}

$spsite.allowUnsafeUpdates = $allowUnsafeUpdates

$rootweb.dispose()
$spsite.dispose()

Get-SPUser –Web $sitename| Set-SPUser –SyncFromAD
Advertisements
Categories: SharePoint 2010 Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: