Boost your shell with reusable code stripped to its essentials


Component Object Model (COM) is the basis for several other Microsoft technologies and frameworks, including OLE, OLE Automation, Browser Helper Object, ActiveX, COM+, DCOM, the Windows shell, DirectX, UMDF and Windows Runtime. ComObjects can be created with the New-Object cmdlet but the problem is that the input and output are provider through the arguments which makes it difficult pipe or assign values the normal way.

Therefor this little Invoke-ComObject wrapper that takes away some overhead and has a more common PowerShell interface.


Function Invoke-ComObject {                                     # https://powersnippets.com/invoke-comobject/
    [CmdletBinding()]Param (                                    # Version 01.01.00, by iRon1
        [Parameter(Mandatory = $true)]$ComObject, [Switch]$Method, [Parameter(Mandatory = $true)][String]$Name, $Value
    If ($ComObject -IsNot "__ComObject") {
        If (!$ComInvoke) {$Global:ComInvoke = @{}}
        If (!$ComInvoke.$ComObject) {$ComInvoke.$ComObject = New-Object -ComObject $ComObject}
        $ComObject = $ComInvoke.$ComObject
    If ($Method) {$Invoke = "InvokeMethod"} ElseIf ($MyInvocation.BoundParameters.ContainsKey("Value")) {$Invoke = "SetProperty"} Else {$Invoke = "GetProperty"}
    [__ComObject].InvokeMember($Name, $Invoke, $Null, $ComObject, $Value)
}; Set-Alias Com Invoke-ComObject


Invoke-ComObject  [-ComObject]  <ComObject|String>  [-Method]  [-Name]  <string>  [[-Value]  <Object>]


[-ComObject] < ComObject|String>
If the supplied argument is a ComObject, the concerned ComObject will be (re)used. If the supplied argument is a String, a new ComObject will be created unless a copy of the concerned ComObject already exist, the original ComObject will be (re)used.

If supplied, the ComObject will be invoked as a method (InvokeMethod). If omitted, the cmdlet will automatically determine whether the property needs to be get (GetProperty) or set (SetProperty) depending on whether a -Value argument is supplied.

[-Name] <string>

  • In case the -Method switch is supplied, the name of the method (InvokeMethod).
  • In case a -Value argument is supplied, the name of the property to be set (SetProperty).
  • In case both the -Method switch and the –Value argument are omitted, the property to be get (GetProperty) and returned by the cmdlet.

[[-Value] <Object>]
The value to be set (SetProperty) or invoked (InvokeMethod).


With this cmdlet you do not require the to create the ComObject first and retrieve e.g. to get the ComputerName (DN) from ADSystemInfo in a simple oneliner:

Com ADSystemInfo ComputerName

To do a the same with the PathName:

$EscapedMode = Com PathName EscapedMode
Com PathName EscapedMode $ADS_ESCAPEDMODE_ON
Com Pathname -Method Set "CN=Ken Dyer,OU=H/R,DC=fabrikam,DC=com", $ADS_SETTYPE_DN
Com Pathname -Method Retrieve $ADS_FORMAT_X500_PARENT
Com PathName EscapedMode $EscapedMode

A name NameTranslate example:

Com -Method NameTranslate Init 1, "domain.com"
Com -Method NameTranslate Set 8, "User001"
Com -Method NameTranslate Get 1

Or if you do want to have multiple instances you can first create the ComObject instance and then supply it to the Com function:

$NameTranslate = New-Object -ComObject NameTranslate
Com -Method $NameTranslate Init 1, "domain.com"
Com -Method $NameTranslate Set 8, "User001"
Com -Method $NameTranslate Get 1

The Invoke-ComObject cmdlet was originally published at StackOverflow

Last updated on 29 Jun 2020
Published on 6 Nov 2017