PowerShell ISE vs cmd.exe

Guy Thomas

Guy Thomas is the writer and editor of Computer Performance, a site dedicated to computer hardware and software maintenance and troubleshooting since 1996. He currently resides in England.

PowerShell's New-Object v Native Operating System Commands

This week I have two objectives. First, to persuade you to launch PowerShell's ISE (Integrated Scripting Engine) instead of cmd.exe's DOS box. Second, to show you how 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

This Week's Secret

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

First, You'll Need PowerShell

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.

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 provides.  That said, the following examples work perfectly well with PowerShell v1.

Command-line Programs to Run in PowerShell ISE

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)

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 life-long habit and launch PowerShell ISE and not cmd.exe.

Introducing New-Object -com

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.  Frankly, this is not a winning long-term technique, but if you stick with me, you will see how new-Object can be employed for a whole host of useful scripting tasks.

First, introduce a PowerShell cmdlet called New-Object and use it to launch cmd.exe.

$Cmdy = new-object -comObject "Shell.Application"
$Cmdy.shellExecute('CMD.exe')

You can use this technique for utilities such as NetSh that won't behave in PowerShell's ISE. 

$Shell = new-Object -comObject WScript.shell
$shell.Run('cmd.exe')
start-sleep 2
$shell.sendkeys("netSh")
Start-sleep 1
$shell.sendkeys("{ENTER}")

Now 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.

$Appy=new-Object -comObject Word.Application
$Appy.visible = $true

# -----------------------------------------------

$Webby = new-Object -comObject InternetExplorer.Application
# $Webby |gm
$Webby.navigate("www.computerperformance.co.uk/powershell/")
$Webby.visible = $true

Two quick notes: All these examples use the -comObject parameter, and if you need help, just type:

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}

You can also remove the hash # $Webby |gm  and discover even more properties for new-Object.

Finally, here is an example which illustrates that PowerShell can do anything that VBScript can do.

$net = New-Object -com WScript.Network
$net.mapnetworkdrive("Y:","\\server\share")

Naturally, you need to amend \\ server\share to the name of a real UNC share on your network. Also, 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.