locked
Release or delete a COM object from powershell RRS feed

  • Question

  • Hi guys, you are great!

    I am writing you regarding this tip:

    https://technet.microsoft.com/en-us/library/ff730962.aspx

    With the Excel tip example works fine...

    When I want release (close) an Outlook object, it still running after "release it" as show the following picture.

    This will run in a task and can't just kill Outlook because the user could be are using it. How can I release/delete the Outlook object to remove it's background process?

    Code:

    Add-Type -assemblyname "Microsoft.Office.Interop.Outlook"
    $Outlook = New-Object -comobject Outlook.Application
    $namespace = $Outlook.GetNameSpace("MAPI")
    $outlookprofilename = $outlookApplication.Application.DefaultProfileName
    Write-host "Default Outlook Profile: $outlookprofilename"
    $Outlook.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
    Remove-Variable Outlook

    Many thanks!


    • Edited by Victor M$ Friday, January 5, 2018 12:29 PM
    Friday, January 5, 2018 12:27 PM

Answers

  • The following is the correct way to code what you are trying to get.

    $Outlook = New-Object -comobject Outlook.Application
    Write-Host $Outlook.DefaultProfileName
    $Outlook.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
    Remove-Variable Outlook
    

    It does not create unneeded objects and gets the profile name directly.


    \_(ツ)_/

    • Marked as answer by Victor M$ Monday, January 8, 2018 4:02 PM
    Friday, January 5, 2018 3:52 PM

All replies

  • You must release ALL objects that have been created in order to release the application object.

    Also the code posted cannot possibly work.


    \_(ツ)_/

    Friday, January 5, 2018 3:49 PM
  • The following is the correct way to code what you are trying to get.

    $Outlook = New-Object -comobject Outlook.Application
    Write-Host $Outlook.DefaultProfileName
    $Outlook.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
    Remove-Variable Outlook
    

    It does not create unneeded objects and gets the profile name directly.


    \_(ツ)_/

    • Marked as answer by Victor M$ Monday, January 8, 2018 4:02 PM
    Friday, January 5, 2018 3:52 PM
  • I've found that if you call the MAPI namespace that you'll need to invoke the LogOff method to get the Outlook taskbar icon to close. Note that it does not instantly close if your #DO STUFF section is relatively short. 

    $Outlook = New-Object -ComObject outlook.application
    $Namespace = $Outlook.getNamespace("MAPI")
    
    #DO STUFF
    
    $Namespace.Logoff()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Namespace)
    Remove-Variable Namespace
    
    $Outlook.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
    Remove-Variable Outlook

    Wednesday, May 23, 2018 7:23 PM
  • The rule is to release all variables that you have used to create references to any COM object in Outlook.  Once all objects are released Outlook will close.  There are numerous articles explain how and why in detail.


    \_(ツ)_/

    Wednesday, May 23, 2018 7:38 PM