How to Unfollow all the sites with powershell script at sharepoint 2013

​If you are looking to script the unfollowing of the sites in SP 2013 then you can use the following PowerShell. You can loop through users to execute the following code. For this sample I have hard coded User Name and Site URL.

$userName = "Contoso\brianc" 

$site = Get-SPSite "http://intranet.contoso.com"

$serviceContext = Get-SPServiceContext($site)

$profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)

if($profileManager.UserExists($userName))
{
     $userProfile = $profileManager.GetUserProfile($userName)
    
     $followingManager = new-object Microsoft.Office.Server.Social.SPSocialFollowingManager($userProfile, $serviceContext)
     
     $followed = $followingManager.GetFollowed("Sites")
     
    $actorInfo = new-object Microsoft.Office.Server.Social.SPSocialActorInfo
    
     foreach($site in $followed)
    {        
        $actorInfo.ContentUri = $site.Uri
        $actorInfo.AccountName = $userProfile.AccountName
        $actorInfo.ActorType = "Site"
        $followingManager.StopFollowing($actorInfo)
    }

}​