'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.



Solution 1:[1]

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
<#
\\[email protected]
#>

$namespace.Folders.Folders.FullFolderPath
# Results
<#
\\[email protected]\Deleted Items
\\[email protected]\Inbox
\\[email protected]\Outbox
\\[email protected]\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...
#>

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 postanote
Solution 2 StuKay