I wanted to upload this script deletes only attachments from a user mailbox.

Good Morning 

This script was very helpful to me and im sure some admin on this forum will have a good use for it as well 

The below is what you will need 

This will only run on power shell 4.0 

you willl also need microsoft.exchange.webservices.dll 

below is the script enjoy, If anyone can help to show me the best way to have the output into a text file and then email it to someone else if not understandable 

#The module lives on the NY share drive
Import-Module -Name “\\homelabco.com\DFSShared\Infra\NY\Documentation\Powershell_Scripts\cleanup_Exchange_attachments\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll”
$exchangesession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri ntex007.homelabco.com/.../ -Authentication Kerberos
Import-PSSession $exchangesession
function doFolder($inFolder) {
write-host “Folder:” $inFolder.displayName
$items = $inFolder.FindItems($itemFilter, $itemView)
foreach ($item in $items.Items) {
write-host $item.Subject
if ((New-TimeSpan (Get-Date) $item.DateTimeReceived).Days -lt 14) {
foreach($attachment in $item.Attachments) {
write-host $attachment.Name
$folders = $service.FindFolders($inFolder.Id, $folderView)
foreach ($folder in $folders.Folders) {

$userinput=Read-Host -Prompt "please enter your username"
$passwordinput=Read-Host "please enter your password"
$credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($userinput,$passwordinput)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
$service.Credentials = $credentials
$service.Url = "ntex007.homelabco.com/.../exchange.asmx"
$mailbox = Get-Mailbox $userinput
$folderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
$folderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow
$itemFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
$itemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10000)
write-host $mailbox.displayName
# you can change the below to "deleteditems" "outbox" or "sentitems"
$folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems, $mailbox.primarySmtpAddress.ToString())
$folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderId)



  • Well, it may be too late note as 15 folks have viewed this.

    However, you look to have real endpoints (real stuff that is showing your internal points) listed here and that is not a good thing.

    You should remove this and sanitize it for security / risk management reasons, then repost if you want to share.

    Of course, no one should be running this script as is, for obvious reasons. Like for instance that it is point to what should be considered and untrusted location on someone else's network.

    As for the output and email question.
    Change this to a function
    Get rid of the Write-Host stuff as the moment you use it, you cannot send data down the pipeline to use / send elsewhere.

    Literally, this...

    write-host $item.Subject

    ... and this....


    ... will do the same thing and write this out to the console. The question is do you really need to and you must not if you are trying to send it else where.

    You can even do stuff like this...

    $ShowProcesses = Get-Process # get all process and store in a variable, but don't show to the console
    ($ShowProcesses = Get-Process ) # same as the above but will output to the console.

    ... to write out to the console

    Drop the write-host, unless you are using color formatting for whatever reason.
    Use Write-Output instead, heck or Write-Verbose. See the PowerShell help files on the topic of their use.
    Now, you should e able to send to a file for email as a attachment.
Reply Children
No Data