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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.