Ezine 194 PowerShell's New-Object v Native Operating System Commands
PowerShell's New-Object v Native Operating System Commands
The more tasks that you delegate to PowerShell, the greater the chance
that you will truly embrace this wonderful command-line technology.
This week I have two objectives, firstly, to persuade you to launch PowerShell's ISE
(Integrated Scripting Engine) instead of cmd.exe's DOS box.
Secondly, PowerShell can mimic VBScript with the command: new-Object -comObject
WScript.Shell. This is useful for launching applications such as
Word or mapping network drives programmatically. Indeed, for those who
have experience of VBScript, then many of its principles transfer to
PowerShell
Topics for PowerShell's New-Object v Native Operating System
Commands
My challenge is for you to abandon the old DOS box and try PowerShell instead.
You will find that old friends such
as 'ipconfig /all' and 'Ping ServerName' behave virtually identically.
While other utilities such as 'NetSh' behave badly when I run them directly within PowerShell,
nevertheless, it is possible to get these command line utilities working
thanks to
PowerShell's 'new-Object -comObject WScript.Shell'.
More good news, 'Route Print', 'Powercfg /?' and
'Shutdown' each work fine
with PowerShell. Another pleasant side-effect of researching the cmdlet
'new-Object' is
that it's great for programmatically launching applications such as Word,
Internet Explorer, or virtually any other executable, for example:
new-object -comObject Word.Application
PowerShell is built-in to Microsoft's latest operating systems:
Windows 7, Vista and Windows Server 2008. In these cases all you
do is navigate to the Control Panel, Programs, and 'Turn Windows feature
on'.
For older operating systems such as XP and Windows Server 2003 you
need to download and install PowerShell together with .Net Framework
from Microsoft's website.
Version 2.0 CTP (Community Technology Preview) 3 is
available for download. You can check which version you have
installed by typing $host at the PowerShell command line. If you
already have version 1 and wish to upgrade to V2 CTP 3, this can be a
challenge, so even Guy had to read the instructions that Microsoft provide.
That said the following examples work perfectly well with PowerShell v1.
Here is a list of the operating system's built-in executables that you
can run in PowerShell. The procedure is just as easy as if you
type them in cmd.exe. Naturally
try them one at a time! Better still, think what each might do,
especially with the last one!
# Type from the PowerShell command line
Ping LocalHost (Ping YourServerName)
Ipconfig /all
Powercfg /?
Route Print
Shutdown /r /m \\localhost (Follow up with shutdown /a)
Let me tell you where I am with PowerShell v Cmd.
These days I am more than happy to use PowerShell for command-line
instructions such as traceRt and RoboCopy. I just need to break a
habit of a lifetime and launch PowerShell ISE (Integrated Scripting
Engine), and not cmd.
As you may know, PowerShell has hundreds of its own verb-Noun
instructions called cmdlets. In this example I just want to introduce
you to the cmdlet that creates a DOS box, which you could then send
commands as though you typed them. To be frank this is not a winning long-term technique, however, if you stick with me, you will see how
new-Object can be employed for a whole host of useful scripting tasks.
Firstly, I want to introduce a PowerShell cmdlet called New-Object and use it to launch cmd.exe.
Thirdly, I want to give this exercise a point by showing how
PowerShell's new-Object could be better used to launch applications such as Word or Internet Explorer.
Note 1: All these example use the -comObject
parameter.
Note 2: We could call for help and examine the
examples by typing: get-Help new-Object -full
What we learn is the -set parameter thus: new-Object
-comobject InternetExplorer.Application ` -set @{navigate="www.computerperformance.co.uk/";
visible = $true}
Note 3: Remove the hash # $Webby |gm and discover
even
more properties for new-Object.
MapNetworkDrive
Finally, here is an example which illustrates that PowerShell can do
anything that VBScript can do.
Note 1: Naturally, you need to amend \\ server\share to the name of a real UNC share on your network.
Note 2: To see if your PowerShell script performs as planned, launch Windows
Explorer.
Summary of PowerShell New-Object -com
The main purpose of this article is to encourage you to use PowerShell's ISE instead of cmd.exe's DOS box.
Then I hope that you will investigate the command: new-Object -comObject
WScript.Shell, for example, Word.Application or WScript.Network.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.