kwizcom banner advertisement

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 in welcome menu. However, To promote the usage of SharePoint social, we deiced to pre-create SharePoint My sites for all users.
create mysite for all users in sharepoint using powershell

Create Mysite for All Users in SharePoint using PowerShell
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!
Create My Site for All Users in SharePoint using PowerShell Create My Site for All Users in SharePoint using PowerShell Reviewed by Salaudeen Rajack on November 03, 2015 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.