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 (opens in new tab).  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"

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

$Shell = new-Object -comObject WScript.shell
start-sleep 2
Start-sleep 1

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

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.

  • I've been scripting exclusively in Powershell for 2 months. I'll never go back to vbscript nor cscript.
  • Shadow703793
    Linux $bash FTW!
  • joeman42
    Does the world need yet another scripting language? I don't see anything particularly unique or compelling in PowerShell. It may be "cleaner" and more elegant than vb/ecma script, but that is because it is newer and has not had the need to accumulate the extensions and baggage of the others. Cmd.exe is sufficient for simple tasks and for more involved work I prefer to use something much less platform specific, Python.
  • randomizer
    It's about time Microsoft tried something new. CMD.exe looks archaic next to Bash and other Unix shells.
  • Parrdacc
    I like Powershell in the fact that Microsoft really needed something like this for a long while. Bash is great. I just think its funny after all these years M$ is finally "starting" to take ques from others on how to go about doing things, I'll be it by adding their own little "touch" to it.
  • I could use cscript in cmd to run vbscript. So what's the advantage again?
  • FlairFan
    I'm a perl programmer from way back (even evangelizing it to the NT folks back when I was still a *nix guy) and have pretty much left it behind. "Does the world need another scripting language?" Maybe not, but WINDOWS sure as **it does. Perhaps 'shell' is where folks are getting hung up. It is an interactive shell (like ksh, bash, etc.) but it's also like Perl, PHP, javascript, etc.

    It's the OBJECT PIPELINE that raises PowerShell above the others.
  • dedhorse
    ParrdaccI'll be it...
    "Albeit," not "I'll be it" :).
  • hellwig
    $Webby = new-Object -comObject InternetExplorer.Application
    # $Webby |gm
    $Webby.visible = $true
    Haha, yes, much simpler than
    c:\> start iexplore.exe "http://www.dummies.com/store/Computers-Internet/Operating-Systems/Windows.html"

    Much. I'm sure powershell has plenty of wonderful attributes, but launching applications isn't one of them.
  • Parrdacc
    dedhorse"Albeit," not "I'll be it" .
    Oh not you too! What is it with all the grammar cops on this lately? Granted that was a pretty bad one :).