Sign in with
Sign up | Sign in

PowerShell ISE vs cmd.exe

PowerShell ISE vs cmd.exe
By

The more tasks that you delegate to PowerShell, the greater the chance that you will truly embrace this wonderful command-line technology.

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.

Ask a Category Expert

Create a new thread in the Streaming Video & TVs forum about this subject

Example: Notebook, Android, SSD hard drive

Display all 21 comments.
This thread is closed for comments
  • 0 Hide
    Anonymous , November 13, 2009 12:21 AM
    I've been scripting exclusively in Powershell for 2 months. I'll never go back to vbscript nor cscript.
  • 3 Hide
    Shadow703793 , November 13, 2009 12:31 AM
    Linux $bash FTW!
  • 3 Hide
    joeman42 , November 13, 2009 2:28 AM
    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.
  • 2 Hide
    randomizer , November 13, 2009 3:52 AM
    It's about time Microsoft tried something new. CMD.exe looks archaic next to Bash and other Unix shells.
  • 2 Hide
    Parrdacc , November 13, 2009 1:09 PM
    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.
  • 0 Hide
    Anonymous , November 13, 2009 1:32 PM
    I could use cscript in cmd to run vbscript. So what's the advantage again?
  • 2 Hide
    FlairFan , November 13, 2009 2:05 PM
    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.
  • 4 Hide
    dedhorse , November 13, 2009 3:59 PM
    ParrdaccI'll be it...


    "Albeit," not "I'll be it" :) .
  • 0 Hide
    hellwig , November 13, 2009 4:20 PM
    Quote:
    $Webby = new-Object -comObject InternetExplorer.Application
    # $Webby |gm
    $Webby.navigate("www.computerperformance.co.uk/powershell/")
    $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.
  • 0 Hide
    Parrdacc , November 13, 2009 4:58 PM
    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 :) .
  • 0 Hide
    Anonymous , November 14, 2009 11:29 PM
    @joeman42

    You can do things in PowerShell with a fraction of the code it takes in VB, especially if you get in the habit of making your functions be pipeline functions. The scripting language itself is like only 15 keywords vs, VB's 130 or so. You don't need any more than the 15 key words, because of the power of accessing everything as an object, the power of the pipelining, and being able to access .NET objects. Also, anything you do in a script can be done in realtime in the shell... so you can test bits of code as you go.

    It's actually superior to Bash scripting, IMO. It's Bash scripting on steroids (for Windows).
  • 0 Hide
    matt87_50 , November 15, 2009 11:48 PM
    I love it being called bash. I don't why it's called that, probably for this reason.

    all I can imagine is "... ... oh, come on ... what? why not?? COME THE F#&K ON! YOU ****ING ****! WHY won't you WORK!? ... ARGAFEWF@R$#2#$@#%! ... Oh thank God, Finally! there, was that good for you? you useless Piece of SHIT!"

    to be fair I've never really used bash, but if cmd was called bash, that 'd be the reason.
  • 0 Hide
    randomizer , November 15, 2009 11:59 PM
    Actually, Bash means Bourne-Again Shell, being based off the old Bourne shell. Personally, I think getting angry at a bunch of text says something about the sanity of the user.
  • 1 Hide
    Anonymous , November 16, 2009 1:29 AM
    To launch a program, you can use ii (Invoke-Item). E.g., to launch a command prompt, type 'ii cmd.exe'.
  • -1 Hide
    tmike , November 16, 2009 3:10 AM
    Was this article really supposed to make someone want to use powershell? Je ne think-so pas. I see that in the time since I last examined it, powershell has become no less a fustercluck than it was then.

    As for bash, its fans are those who have only been exposed to UNIX via Linux and its lamed-down hacked-up shells. bash is as sorry a substitute for ksh93 as Linux is for UNIX.

  • 0 Hide
    Anonymous , November 16, 2009 12:14 PM
    @hellwig
    this is also possible in PowerShell, as it is a shell you can just start programs directly, this example was about using COM.

    iexplore.exe "http://www.dummies.com/store/Computers-Internet/Operating-Systems/Windows.html"
  • 0 Hide
    jb9876 , November 16, 2009 7:13 PM
    I think some are missing the point of PowerShell.
    1. Its more then a shell, it has a language.
    2. Windows needs it.
    3. Like flairflan said above, "It's an OBJECT PIPELINE".
    4. It was designed to work in the Windows world, thus very different requirements then *nix shells. For example, needs to talk to Objects without having to convert to text and back.
    5. Used concepts from prior scripting languages, even Perl.
    6. Built into latest Windows OS. So admin scripting is doable.
    7. It's actually very expressive and easy to learn.
    8. Good books on it already.
    9. It is not perfect. What is?


  • 0 Hide
    TwoDigital , November 17, 2009 2:38 PM
    How about a native 32-bit or 64-bit BASH shell with true redirection? I agree that powershell scripting is kind of cool, but if I were to replace cmd.exe I'd want to replace it with Cygwin BASH with xterm and fg/bg process switching.
  • -1 Hide
    b3orion , November 20, 2009 12:57 AM
    @ tmike.
    I like your french... "Je ne think so pas"...
    Haha!
  • 0 Hide
    kwezzy , April 23, 2010 9:04 AM
    dedhorse"Albeit," not "I'll be it" .

    dude have you ever heard of malapropism???? the " i'll be it" is a malapropism for "albeit" so he was correct...
Display more comments
Tom’s guide in the world
  • Germany
  • France
  • Italy
  • Ireland
  • UK
Follow Tom’s guide
Subscribe to our newsletter