Tuesday, November 3, 2015

Create My Site for All Users in SharePoint using PowerShell

SharePoint My Sites are automatically created when the first time Users click on the My Content link. However, To promote the usage of SharePoint social, we deiced to pre-create SharePoint My sites for all users.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
Function Create-MySite($MySiteHost, $UserAccount) 
{
    #Get Objects
    $ServiceContext  = Get-SPServiceContext -site $MySiteHost
    $UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

    #Check if user Exists
    if ($UserProfileManager.UserExists($UserAccount)) 
    {
        #Get the User Profile
        $UserProfile = $UserProfileManager.GetUserProfile($UserAccount)
        
        #Check if User's My site is Cretaed already
        if($UserProfile.PersonalSite -eq $Null)
        {
            $UserProfile.CreatePersonalSite()
            write-host "My Site Created Successfully for the User!" -f Green
        }        
    }
    else
    {
        write-host "$($UserAccount) Not Found!" -f Red
    }
}

Create-MySite "https://mysite.crescent.com" "Crescent\Salaudeen"

SharePoint: Create Mysite for all users using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$MySiteHost = "https://mysite.crescent.com"
 
#Get Objects
$ServiceContext  = Get-SPServiceContext -site $MySiteHost
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get All User Profiles
$UserProfiles = $UserProfileManager.GetEnumerator()

#Iterate through each profile
foreach ($Profile in $UserProfiles)
{
    if($Profile.PersonalSite -eq $Null)
    {
        #Check if User's My site is Cretaed already
        if($Profile.PersonalSite -eq $Null)
        {
            #$UserProfile.CreatePersonalSite()
            write-host "My Site Created Successfully for the User:" $Profile["AccountName"] -f Green
        }
        else
        {
            write-host "My Site Already Exists for the User:" $Profile["AccountName"] -f Red
        } 
    }
}
BTW, I've tested this script in SharePoint 2013 to create mysite for all users!



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...