Displaying Email subjects from a particular folder

Hi Team ,

I want to list the email subjects from the Inbox folders . I have written a code but it is taking me only to the folder level .Can you please tell me how to display the only email subjects from the folders .Please find the below code

$outlook = new-object -com outlook.application;

$ns = $outlook.GetNameSpace(“MAPI”);

$inbox = $ns.GetDefaultFolder(6)

$targetfolder = $inbox.Folders

$targetfolder |select -Property Name

If i execute the above code am getting the below folders that exists in my inbox .My queury is how to get only the email subjects that exists in these folders .

Name ---- folder1 folder2 folder3 folder4

@Nikhileshwar - Did you get the solution for this ? if so please share it with us which will be helpful for others else please update this thread with latest update.

Hi Prasoon

$outlook = new-object -com outlook.application;
$namespace = $outlook.GetNameSpace(“MAPI”);

The below command will display the mail locations containing the inbox and sent items
$namespace.folders|select -property folderpath

If i want to display the inbox from the first mail location

$namespace.folders.item(1).folders.item(‘Inbox’)

If i want to display the email subjects,receivedtime for the mails of the subfolder of this inbox you have to give

$namespace.folders.item(1).folders.item(‘Inbox’).folders.item(‘sub-folder’).items|select -property subject,receivedtime

Thanks for sharing the solution. I will format it for others.

$outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
#The below command will display the mail locations containing the inbox and sent items
$Namespace.Folders | Select-Object -Property FolderPath
#If i want to display the inbox from the first mail location
$Namespace.Folders.Item(1).Folders.Item('Inbox')
#If i want to display the email subjects,receivedtime for the mails of the subfolder of this inbox you have to give
$Namespace.Folders.Item(1).Folders.Item('Inbox').Folders.Item('sub-folder').Items | Select-Object -Property Subject,ReceivedTme 

[quote quote=118092]Thanks for sharing the solution. I will format it for others.

PowerShell
9 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
$outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
#The below command will display the mail locations containing the inbox and sent items
$Namespace.Folders | Select-Object -Property FolderPath
#If i want to display the inbox from the first mail location
$Namespace.Folders.Item(1).Folders.Item('Inbox')
#If i want to display the email subjects,receivedtime for the mails of the subfolder of this inbox you have to give
$Namespace.Folders.Item(1).Folders.Item('Inbox').Folders.Item('sub-folder').Items | Select-Object -Property Subject,ReceivedTme
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

Mr. Prasoon,

Thanks for sharing the script. However, it does not work, from the PowerShell ISE running as Administrator:

New-Object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)). At line:2 char:12 + $outlook = New-Object -ComObject Outlook.Application + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At line:3 char:1

  • $Namespace = $Outlook.GetNameSpace(“MAPI”)
  • CategoryInfo : InvalidOperation: (:slight_smile: , RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

Start 32bit PowerShell or PowerShell ISE from C:\Windows\Syswow64\WindowsPowerShell\v1.0 . If this works, you MS office is 32 bit.