Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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)
The first step is to create a new object and then assign it to a variable, for example:

# PowerShell Word.Application Create and Launch
Clear-host
$MsApp = New-Object -comObject Word.Application
$MsApp.Visible = $true

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

Clear-Host
$MsApp.Quit()

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
Clear-Host
$MsOutlook = New-Object -comObject Outlook.Application
$Namespace = $MsOutlook.GetNamespace("MAPI")
$Folder = $Namespace.GetDefaultFolder("olFolderInbox")
$Explorer = $Folder.GetExplorer()
$Explorer.Display()

Note 3:  For applications such as Outlook you could employ a completely different cmdlet and call for:
Start-Process Outlook.exe.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

3) Working With Outlook in PowerShell

# PowerShell Retrieves Outlook Email Subject
Clear-Host
$MsOutlook = New-Object -comObject Outlook.Application
$sentMail = $MsOutlook.Session.GetDefaultFolder(6)
$sentMail.Items | select -last 5 TaskSubject

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
CONST olFolderOutbox = 4
CONST olFolderSentMail = 5
CONST olFolderInbox = 6
CONST olFolderCalendar = 9
CONST olFolderContacts = 10
CONST olFolderJournal = 11
CONST olFolderNotes = 12
CONST olFolderTasks = 13
CONST olFolderDrafts = 16

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.

# ShellExplore.ps1
# Opening Explorer using PowerShell
# Author Guy Thomas http://computerperformance.co.uk/
# Version 2.5 - October 2010
# Sets the Drive
$Drive = "C:\windows"
# Launches the Explorer
$ShellExp = new-object -comObject Shell.Application
$ShellExp.explore($Drive)

Research Shell.Application  Methods and Properties

Let us investigate the methods and properties available to our shell object with Get-Member

# PowerShell Shell.Application properties
$ShellExp = new-object -comObject Shell.Application
$ShellExp | Get-Member

In particular, lookout for the methods: 'Open' and 'Explore', because these are the methods that we are going to apply to our object.

®

Name MemberType
------------------- ----------
AddToRecent Method
BrowseForFolder Method
CanStartStopService Method
CascadeWindows Method
ControlPanelItem Method
EjectPC Method
Explore Method
ExplorerPolicy Method
FileRun Method
FindComputer Method
FindFiles Method
FindPrinter Method
GetSetting Method
GetSystemInformation Method
Help Method
IsRestricted Method
IsServiceRunning Method
MinimizeAll Method
NameSpace Method
Open Method
RefreshMenu Method
ServiceStart Method
ServiceStop Method
SetTime Method
ShellExecute Method
ShowBrowserBar Method
ShutdownWindows Method
Suspend Method
TileHorizontally Method
TileVertically Method
ToggleDesktop Method
TrayProperties Method
UndoMinimizeALL Method
Windows Method
WindowsSecurity Method
Application Property
Parent Property

ComObject in PowerShell 3.0 Logon Script

The purpose of this script is to map a network drive in PowerShell.  Observe how the ComObject can also act as a wrapper for the WScript.Network methods.

# PowerShell v 3 Logon Script Example
$Net = $(New-Object -ComObject WScript.Network)
$Net.MapNetworkDrive("P:", "\\YourMachine\Stuff")

See more on PowerShell 3.0 logon scripts.

Summary of -comObject and Office Applications

We have investigated how to use PowerShell's New-Object to create Microsoft Office objects such as Excel.Application or Outlook.Application.  Incidentally, this PowerShell technique of replaces many of the tasks previously undertaken by VBScript's CreateObject. 

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials:

PowerShell Home   •Shell Application  • New-Object   • PowerShell create shortcut

PowerShell Logon Script   • Map Network Drive   • PowerShell add printer   • PowerShell -com

Invoke-Expression   • Invoke-Command 

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: