PowerShell New-Object -ComObject
Scripting COM Shell Objects - Launch Office Programs
I will show you how to use PowerShell's New-Object to create Microsoft Office objects such as Word.Application or Outlook.Application. Incidentally, this PowerShell technique of replaces many of the tasks previously undertaken by VBScript's CreateObject.
Topics for PowerShell's New-Object -ComObject
PowerShell's New-Object -ComObject
All COM objects are created through the command: New-Object. There are dozens of applications that you can specify with the -ComObject parameter. For example, Excel.Application or Outlook.Application
1) Create the object (New-Object)
# PowerShell Word.Application Create and Launch
Note 1: Observe how the parameter -ComObject handles the application. Incidentally, -ComObject is often abbreviated to -Com.
Note 2: You could substitute Excel for Word, however, Outlook would not work, but see example 2 for a successful technique.
1a) Closing Word
2) Starting Outlook
Using PowerShell to launch Outlook is a little more difficult than Word or Excel. Here is a method which gets the namespaces then displays the inbox.
# PowerShell Outlook.Application and GetNamespace
Note 3: For applications such as Outlook you could
employ a completely different cmdlet and call for:
Here is an utility where you can review firewall settings such as access control lists (ACL), or troubleshoot problems with network address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include managing requests to change your firewall settings, testing firewall rules before you go live, and querying settings with the browser's powerful search options.
Guy recommends that you download a copy of the SolarWinds free Firewall Browser.
3) Working With Outlook in PowerShell
# PowerShell Retrieves Outlook Email Subject
Note 4: The script connects to Default Folder 6 which corresponds to the inbox, from there it retrieves the last 5 items which are the Subjects of the newest emails.
Other useful Outlook Const values
CONST olFolderDeletedItems = 3
PowerShell Script to Explore with the Windows Explorer
The idea behind a second version of opening the Windows Explorer is to give you perspective. By changing a few items, I hope that it gives you extra understanding, also more ideas for your own situation. In the example below I have introduced a variable $Drive to hold the value for the folder, which you want explorer to view. Note also how I have changed .open("D:") to .explore("C:\windows"). For this script to work, you need to have a \windows folder on your c: drive, fortunately, this is the default location for this system folder.
This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.
There are so many good gadgets; it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer's Toolset now!
Let us investigate the methods and properties available to our shell object with Get-Member
# PowerShell Shell.Application properties
In particular, lookout for the methods: 'Open' and 'Explore', because these are the methods that we are going to apply to our object.