PowerShell Function: Remove-ADPhoto

Since we now have a function to add a photo to a User Account object in Active Directory and one to show the photo, anybody care to guess what the Remove-ADPhoto function does?

If you didn’t answer something along the lines of “removes the photo from the User Account object in Active Directory”, you’re in the wrong place.

    ##--------------------------------------------------------------------------
    ##  FUNCTION.......:  Remove-ADPhoto
    ##  PURPOSE........:  Removes the picture stored in an Active Directory
    ##                    account.
    ##  REQUIREMENTS...:  PowerShell v2.0, Windows Server 2008 or newer Active
    ##                    Directory.
    ##  NOTES..........:
    ##--------------------------------------------------------------------------
    Function Remove-ADPhoto {
        <#
        .SYNOPSIS
         Removes the picture stored in an Active Directory account.
        .DESCRIPTION
         Clears the thumbnailPhoto attribute of the specified user's Active
         directory account
        .PARAMETER UserName
         The User logon name for the account the picture will be removed from.
         Alias: -un

        .EXAMPLE
         C:\PS>Remove-ADPhoto user1

         This example will clear the thumbnailPhoto attribute in Active
         Directory for the user account with the User logon name of "user1".

        .NOTES
         NAME......:  Remove-ADPhoto
         AUTHOR....:  Joe Glessner
         LAST EDIT.:  29NOV11
         CREATED...:  28NOV11
        .LINK
         https://joeit.wordpress.com/
        #>

        [CmdletBinding()]
            Param (
                [Parameter(Mandatory=$True,
                    #ValueFromPipeline=$True,
                    #ValueFromPipelineByPropertyName=$True,
                    Position=0)]
                [Alias('un')]
                [String]$UserName
            )#End: Param

        ##----------------------------------------------------------------------
        ##  Search AD for the user, set the path to the user account object.
        ##----------------------------------------------------------------------
        $Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
        $Searcher.Filter = "(&(ObjectClass=User)(SAMAccountName= $UserName))"
        $FoundUser = $Searcher.findOne()
        $P = $FoundUser | Select path
        Write-Verbose "Retrieving LDAP path for user $UserName ..."
        If ($FoundUser -ne $null) {
            Write-Verbose $P.Path
        }#END: If ($FoundUser -ne $null)
        Else {
            Write-Warning "User $UserName not found in this domain!"
            Write-Warning "Aborting..."
            Break;
        }#END: Else
        $User = [adsi]$P.path

        ##----------------------------------------------------------------------
        ##  Attach the picture to the AD account object (remove it if the
        ##  -remove switch is set).
        ##----------------------------------------------------------------------
        Write-Verbose "Removing photo from user account $UserName ..."
        $User.Properties["thumbnailPhoto"].Clear()
        #$User.Properties["jpegPhoto"].Clear()
        $User.CommitChanges()
        Write-Verbose "Attribute cleared!"
    }#END: Function Remove-ADPhoto

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: