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
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 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:
- Bulk-import new users into Active Directory.
- Seek and zap unwanted user accounts.
- 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)
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.
|