Prof. Powershell

Prof. PowerShell: COM-munication

Some COM objects work just fine in PowerShell -- maybe better.

Even though PowerShell is based on .NET Framework, it supports legacy approaches to systems management, including how it uses COM objects. If you've written VBScripts, you've used COM objects for things like the FileSystemObject, the Shell and Network. The good news is that you can use those objects in PowerShell, too. The best news is that you can work with the objects interactively without a script!

Open a PowerShell prompt and type:

PS C:\> $shell=new-object -COM 
"wscript.shell"

Now pipe $shell to Get-Member and you should see all of the methods and properties you were used to working with in VBScript. Try this:

PS C:\> $shell.specialfolders

You should see all the special Windows folders. Unfortunately, not all COM functionality translates properly to PowerShell. In VBScript I could use $shell.specialfolders("desktop") to retrieve just the desktop folder, but not in PowerShell. But you can execute methods:

PS C:\> 
$Shell.regread("HKLM\Soft 
ware\microsoft\Windows 
NT\CurrentVersion\Registere 
dOwner")
Admin
PS C:\> $Shell.regwrite("HKLM\Soft 
ware\microsoft\Windows 
NT\CurrentVersion\Registere 
dOwner","Jeff Hicks")
PS C:\> $Shell.regread("HKLM\Soft 
ware\microsoft\Windows 
NT\CurrentVersion\Registere 
dOwner")Jeff Hicks

Having access to these methods provides a transition to moving completely to PowerShell. But sometimes the older ways still work just fine:

PS C:\> $network=new-object -COM "wscript.net 
work"
PS C:\> $network.EnumNetworkDrives()
P:
\\pluto\files

Or the COM objects offer functionality that you don't have in PowerShell, or perhaps an easier approach:

PS C:\> $fso=new-object -com "scripting.filesyste
mobject"
PS C:\> $fso.drives | where {$_.drivetype -eq 2} | 
Select Path,AvailableSpace,FreeSpace,TotalSize

Path  AvailableSpace   FreeSpace   TotalSize
--    --------------   ---------   ---------
C:    136613888        136613888   4285337600
E:    256651264        256651264   1069253632

Remember we're still working with objects, so we can leverage the PowerShell pipeline and take advantage of the best of both worlds. You'll also discover that if you want to work with other apps -- such as Word, Excel or even Apple iTunes -- you can with PowerShell and its support for COM objects. Create a COM object, pipe it to Get-Member to discover its methods and properties and start using it immediately in PowerShell without writing a single line of script.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

Featured

  • Office Mobile Apps To End as Microsoft Highlights New Office App

    Microsoft plans to end support for Windows 10 Mobile applications on Jan. 12, 2021, according to a Friday announcement.

  • Is Microsoft Finally Reinventing Office?

    Microsoft is testing out a new technology called "Fluid Framework." It could mean that Brien's dream of one Office app to rule them all might soon become reality.

  • Azure Active Directory Connect Preview Adds Support for Disconnected AD Forests

    Microsoft on Thursday announced a preview of a new "Cloud Provisioning" feature for the Azure Active Directory Connect service that promises to bring together scattered Active Directory "forests."

  • Microsoft Defender ATP Gets macOS Investigation Support

    The endpoint and detection response (EDR) feature in Microsoft Defender Advanced Threat Protection (ATP) has reached the "general availability" stage for macOS devices.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.