PowerShell : Twitter Auto Unfollow


Anybody who uses Twitter has heard of auto-follow services for Twitter.

Below is a PowerShell script I used to auto UNfollow.  The script goes through the people you’re following and eliminates “idle” or “spam” friends.  An idle friend is determined by a low friend count or low status count on an account that has been in existance for at least a couple months (configurable).  A spam friend is determined by a high friend count but a low follower count (by default, the script uses a 15-1 ratio as the max).

I would appreciate any good tweaks or improvements you might like to share!  In particular I wasn’t sure how to deal with Twitter’s rate limiting (150 get requests per hour).  For now I just catch the exception when it occurs and sleep for an hour before resuming.

#this script goes through the people you're following and eliminates "idle" or "spam" friends.
#an idle friend is determined by low friends or statuses on an account that has been in existance for a while
#a spam friend is determined by high friends but low followers

param([int] $minfriends = 20,
        [int] $minstatuses = 20,
        [int] $minaccountage = 60, #days
        [int] $maxratio = 15,
        [switch] $silent = $false #whether or not to prompt before unfollowing

function unfollow($userid, $userinfo) {
    do {
        Write-Host "Unfollowing User $($userinfo.user.screen_name): $($userinfo.user.friends_count) friends/$($userinfo.user.followers_count) followers/$($userinfo.user.statuses_count) statuses [y/n/l]: " -noNewLine -foregroundColor Yellow
        if ($silent -eq $false) {
            $cmd = Read-Host
        if ($silent -eq $true -or $cmd -eq "y") {
            $wc = new-object net.webclient
            $wc.Credentials = $global:twittercred
            $unfollo = $wc.UploadString("http://www.twitter.com/friendships/destroy/$($userid).xml", "")                
        if ($cmd -eq "l") {
            Start-Process "http://www.twitter.com/$($userinfo.user.screen_name)"
    } while ($silent -eq $false -and $cmd -eq "l")

#enter your Twitter credentials for retrieving your friends list
$global:twittercred = Get-Credential

#retrieve the full friends list
$wc = new-object net.webclient
$wc.Credentials = $global:twittercred
$response = [xml]($wc.DownloadString("http://www.twitter.com/friends/ids.xml"))

#now loop through each friend
$response.ids.id | % {
        #get detailed info about this user
        $showuser = [xml]($wc.DownloadString("http://www.twitter.com/users/show.xml?id=$($_)"))
    } catch {
        Write-Host "Twitter rate limit reached (150 gets/hour).  Pausing for one hour..." -foregroundColor Red -noNewLine
        Start-Sleep -Seconds 3600
    #determine if they are worthy of following:
     $datecreated = [DateTime]::ParseExact($showuser.user.created_at, "ddd MMM dd H:mm:ss +0000 yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
     $accountage = ([DateTime]::Now).Subtract($datecreated).Days    
    $statusescount = [int]$showuser.user.statuses_count
    $friendscount = [int]$showuser.user.friends_count
    $followerscount = [int]$showuser.user.followers_count
    if (($accountage -ge $minaccountage) -and ($friendscount -lt $minfriends -or $statusescount -lt $minstatuses)) {
        #unfollow idle friends:
        unfollow $_ $showuser
    elseif (($accountage -ge $minaccountage -and $followerscount -eq 0) -or ($followerscount -gt 0 -and $friendscount / $followerscount -gt $maxratio)) {
        #unfollow spam friends:
        unfollow $_ $showuser
    else {
        Write-Host "Good friend $($showuser.user.screen_name): $($friendscount)/$($friendscount)/$($statusescount): "

Leave a Reply

Your email address will not be published. Required fields are marked *