In Outlook client you can configure multiple email accounts as per your need. You can add your primary work email as a default profile and configure personal email as a secondary profile. Sometimes you may need to fetch details of all the configured profiles, you can use outlook client itself for this need, but the easiest way is Powershell to retrieve all the properties from multiple accounts.
Open the Powershell console and run the below command to list DisplayName and SmtpAddress of all accounts.
$outlookApplication = New-Object -ComObject 'Outlook.Application' $accounts = $outlookApplication.Session.Accounts $accounts | Select DisplayName, SmtpAddress
Use the below command to list all the available properties.
$accounts | FL
The below command returns the default profile name
$outlookApplication.Application.DefaultProfileName
Note: The above commands will work only in Outlook client installed machine as it uses the interface library “Microsoft.Office.Interop.Outlook.Application” which works under the logged-in user’s outlook context.
Since the above commands are working under the logged-in user’s outlook context, you have to open the Powershell without Run as administrator privilege. If you have opened the Powershell console with Run as administrator privilege, then you will get the below error :
PS C:WINDOWSsystem32> $outlookApplication = New-Object -ComObject 'Outlook.Application' 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:1 char:23 + $outlookApplication = New-Object -ComObject 'Outlook.Application' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
Hello,
Thank you for this useful script. Is there anyway to run this script with “Run as Administrator” privilege?