List Senders from a Shared Mailbox Folder (Recursively) - powershell

I am trying to get a full list of senders to an specific shared mailbox i have access to (vía Outlook).
So far I am using this small but handy script to choose the selected folder as I have no idea on how to call as a parameter (Param) the folder I want to check:
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$Inbox = $namespace.pickfolder()
$Inbox.items | Select-Object -Property SenderName
After that I filter the output depending on the info I need (in this case, senders).
This forces me to choose the folder MANUALLy, I just want to set it as a parameter to the script.
How should I pass the specific folder as a parameter?.
Thanks in advance and best regards.

If I understood that correctly, you yould create a function where you pass the Inbox Name
function GetSenders
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string]$Inbox
)
Begin
{
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
}
Process
{
"Your Script here - What should happen with the Inbox?"
}
End
{
Write-Host "Script has Finished"
}
}
When you load this Function, you can just write GetSenders -Inbox "InboxName"

Related

Is there a way to refresh an Outlook Inbox subfolder in powershell?

I have a powershell script to get e-mails from an Outlook Inbox subfolder, but it only returns e-mails after three or more executions.
Is there a way to prompt Outlook to refresh the Inbox subfolder before the script prompts the scan?
I am accessing the mailbox through another machine to guarantee the emails are on the subfolder and the flags are cleared.
Here it is a sample of the script:
# Add Interop Assembly
Add-type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
# Create outlook COM object to search folders
$Outlook = New-Object -ComObject Outlook.Application
$OutlookNS = $Outlook.GetNamespace("MAPI")
$OutlookNS.Logon($null, $null, $false, $false);
$OutlookNS.SendAndReceive($false) | Wait-Job
Write-Output "Getting an email from Outlook..."
$Email = $OutlookNS.Folders.Item($Account).Folders.Item($Folder).Items | ? { ($_.FlagStatus -match 0) } | Sort ReceivedTime | Select -First 1
if($Email){
Write-Output $Email.Subject
} else {
Write-Output "No data"
}
...

PowerShell outlook shows saved name instead of full email address

I am trying to retrieve sent emails from my outlook using powershell. When i run below code, it pulls the details, but it prints the name of the person email sent to instead of their email address. How can i get user full email address instead of their name?
Function Get-OutlookSentItems {
Add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderSentMail)
$folder.items |
Select-Object -Property Subject, SentOn, Importance, To
} #end function Get-OutlookSentItems
Get-OutlookSentItems
Current output Test,user
Expected output testuser#test.com

powershell script to list all sub-folders in an Outlook Inbox

New powershell user here. I want a list of all folders and subfolders and subsubfolders etc. from an Outlook Inbox
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
Get-ChildItem -Directory $namespace
The term 'FileInfo' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
Outlook folders are not directory items, they are objects in your Outlook profile.
So, you can't do this...
Get-ChildItem -Directory $namespace
... since that is for the Windows file system.
So, you should going after the folder object(s):
### Messing with Outlook folders
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$namespace.Folders
# Results
<#
$namespace.Folders
Application : Microsoft.Office.Interop.Outlook.ApplicationClass
Class : 2
Session : Microsoft.Office.Interop.Outlook.NameSpaceClass
Parent : Microsoft.Office.Interop.Outlook.NameSpaceClass
DefaultItemType : 0
DefaultMessageClass : IPM.Note
Description :
EntryID : 0000000070244...
Folders : System.__ComObject
Items : System.__ComObject
Name : ...
#>
$namespace.Folders.FullFolderPath
# Results
<#
\\user01#contoso.com
#>
$namespace.Folders.Folders.FullFolderPath
# Results
<#
\\user01#contoso.com\Deleted Items
\\user01#contoso.com\Inbox
\\user01#contoso.com\Outbox
\\user01#contoso.com\Sent Items
...
#>
($folder = $namespace.getDefaultFolder)
# Results
<#
OverloadDefinitions
-------------------
Microsoft.Office.Interop.Outlook.MAPIFolder GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders FolderType)
Microsoft.Office.Interop.Outlook.MAPIFolder _NameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders FolderType)
#>
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$folder.items
# Results
<#
Yadds...
Yadda...
Yadda...
#>
Following code (PS version 7.1.3) will list all Outlook folders (Plus total number of items in each folder) in alphabetical order and indent each each sub-folder for easier reading.
Add-Type `
-LiteralPath "C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Outlook\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Outlook.dll" `
-ReferencedAssemblies "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$ns = $Outlook.GetNameSpace("MAPI")
Function Listfolders
{
param($Folders, $Indent)
ForEach ($Folder in $Folders | sort-object name)
{
write-host $Indent$($Folder.Name)" ("$($Folder.Items.Count)")"
Listfolders $Folder.Folders $Indent" "
}
}
ListFolders $ns.Folders ""
You may, or may not, want to stop the Outlook process after you have completed running this.
Note: This will close Outlook if it was already running.
Get-Process "*outlook*" | Stop-Process

How to Send email from Draft using Powershell?

I'm trying to automatically send all email saved, which are available in "Draft" using powershell.I've seen VBA can do, but powershell is the future. Sharing code will help other peer to tweak.
#Send all items in the "Drafts" folder that have a "To" address filled in.
#Setup Outlook
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$myOutlook = $Outlook.Application
$myNameSpace = $myOutlook.GetNamespace("MAPI")
$myFolders = $myNameSpace.Folders
#Set Draft Folder.
$myDraftsFolder = $myFolders("rsprebitz#idafoundation.org").Folders("Drafts")
#Loop through all Draft Items
$myDraftsFolder.Items|foreach-object {
#Check for "To" address and only send if "To" is filled in.
If ($_.To.trim().length -gt 0) {
#Send Item
$_.Send
}
}
try this
VWP.CS's answer was close but had to make a slight change to get it to work for me:
# Initialize components
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
# Set drafts folder
$myDraftsFolder = $namespace.Folders.Item('yourmailboxname').Folders.Item('Drafts')
# Loop through all Draft Items
$myDraftsFolder.Items | foreach-object {
# Check for "To" address and only send if "To" is filled in.
If ($_.To.trim().length -gt 0) {
#Send Item
Write-Verbose "Sending..." -Verbose
$_.Send()
}
}

How to Set VotingResponses in Outlook via Powershell

I am trying to issue approvals on mailbox items via Powershell but can't seem to find the right method to do so. Here is the code I have used thus far.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$recipient = $namespace.CreateRecipient('othermailbox#example.local')
$recipient.Resolve();
$sharedFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInBox)
$sharedFolder.Items
The member objects from $sharedFolder. Items contain string properties around VotingOptions and VotingResporse but I don't see a method to perform an approved vote. Any help is appreciated as there are hundreds of items I need to bulk approve. Thanks.
Loop through the items, for each item read the VotingOptions property, call MailItem.Reply (returns the new item), set the VotingResponse property on the reply message, call Send.
This is the code I ended up going with based on Dmitiry's input.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$recipient = $namespace.CreateRecipient('othermailbox#example.local')
$recipient.Resolve()
$sharedFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInBox)
$messages = $sharedFolder.Items | Where-Object { $_.subject -eq "Approval Required" }
foreach ($message in $messages)
{
$reply = $message.Reply()
$reply.VotingResponse = "Approve"
$reply.Send()
}