Using COM Objects to Say "Hi!"

by Dec 3, 2008

If you have ever written scripts using VBScript, you probably know COM objects which are DLLs and work like command libraries. You can use COM objects in PowerShell, too. Simply use New-Object and the parameter -COMObject to instantiate the COM library you need.

For example, on XP and above, the COM library SAPI.SpVoice makes your computer speak to you:

$sam = New-Object -comObject SAPI.SpVoice
$sam.Speak('How are you doing, dude?')
$sam.Speak((Get-Date))

If you'd like to know what COM objects live on your computer, look them up! They are all registered in your Windows registry. However, only a fraction of the listed COM libraries are really useful:

Dir registry::HKEY_CLASSES_ROOT\CLSID -include ProgID -recurse | Foreach-Object { $_.GetValue("")}