Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've managed to import user profiles from our AD. Now I want to add all the imported user profiles into a specific SharePoint group, let's say Visitors, and doing this by using the People Picker takes ages. So I was wondering if there was a way of doing this by using Powershell? It would also be nice if the script could run like once a day so that any newly imported user profiles would get added to the group aswell, this can be done thru a timer job right?

Update: I ran the script again after editing $User = $OpenWeb.Users to $User = $OpenWeb.SiteUsers (before that I only got one profile and that was the one was I was logged in as) and this time it semi-worked but the only users that was added to the group were users already added to other groups, not all the users from the user profile database (I'm guessing from the SyncDB?). I can find them if I search for specific profiles in CA -> Manage Service Applications -> User Profile Service Application -> Manage User Profiles, so I know that they are there. Do I have to edit the script in some way in order to get the profiles from the SyncDB (which is where I'm guessing that they are stored)?

share|improve this question

1 Answer 1

The powershell script should be like this:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); 
$OpenWeb = $SpSite.OpenWeb(); 
$TheNewGroup = $OpenWeb.SiteGroups | Where-Object {$_.Name -match "The New Group"}; 
$OpenWeb.Dispose(); 
$SPSite.Dispose()

$SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); 
$OpenWeb = $SpSite.OpenWeb(); 
$User = $OpenWeb.Users | Select Name, Email, LoginName; 
$OpenWeb.Dispose(); 
$SPSite.Dispose()


$User | ForEach-Object {
    $TheNewGroup.AddUser(
        $_.LoginName,
        $_.Email,
        $_.Name,
        ""
    )
}

Ok, so it is possible to add powershell to a timerjob, see link below (from tarjeieo).

share|improve this answer
1  
It's possible alright; ikarstein.wordpress.com/2011/08/04/… –  tarjeieo Oct 25 '11 at 8:13
    
Please dont just link to external content. We like to keep all the good stuff in our site -external sites often tend to have a short life span... Post relevant facts in answer and then link to source please –  Anders Rask Oct 25 '11 at 8:21
1  
you can also just create a Schedule on your application server that calls either PowerShell directly or through a cmd/batch file calls PowerShell and the appropriate .ps1 file –  Anders Rask Oct 25 '11 at 8:38
    
Follow-up question to the script above: if I wanted to get all the user properties do I just need to remove the Select at $User or do I have to add all the properties I want imported? –  K.W Oct 25 '11 at 11:55
    
you could omit the whole | select... part. Then you would receive SPUser objects. Alternatively select the ones you need or * –  Anders Rask Oct 25 '11 at 12:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.