๐Ÿ‡ฏ๐Ÿ‡ต ๆ—ฅๆœฌ่ชž | ๐Ÿ‡บ๐Ÿ‡ธ English | ๐Ÿ‡ช๐Ÿ‡ธ Espaรฑol | ๐Ÿ‡ต๐Ÿ‡น Portuguรชs | ๐Ÿ‡น๐Ÿ‡ญ เน„เธ—เธข | ๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡

Let's Compare and Understand the Differences Between PowerShell and cmd [With Diagrams]

When you think of the "black screen" on Windows, many people might picture the old "Command Prompt (cmd.exe)." However, what we've been learning in the articles so far is the more modern and powerful "PowerShell," characterized by its blue background. These two, while similar in appearance, are actually completely different things.

You might be wondering, "Why are there two types?", "Which one should I use in the end?", "Is cmd obsolete now?" This article will answer those questions by comparing the long-standing rivals (?), PowerShell and Command Prompt, with concrete tasks, explaining the differences in a way that is easy for beginners to understand.

By the time you finish this article, you should have a clear understanding of the overwhelming benefits and why you should choose PowerShell. Let's see for ourselves the difference between Command Prompt, the "hammer," and PowerShell, the "electric multi-tool"!


The Biggest and Most Decisive Difference: "Objects" vs. "Text"

The most fundamental difference between PowerShell and cmd is the format of the information returned by commands. Understanding this is the key to understanding the difference between the two.

The Worldview of Command Prompt (cmd): Everything is "Text"

When you type a command like dir in Command Prompt, what appears on the screen is just "text (a string)" formatted for human reading. It's like a list printed on paper. It is very tedious and requires complex string processing to extract just the "filename" or "last modified date" from this list for use in the next process.

C:\> dir
 Volume in drive C is Windows
 Volume Serial Number is XXXX-XXXX

 Directory of C:\

07/07/2024  09:00 AM              Program Files
07/07/2024  09:05 AM              Users
07/07/2024  09:10 AM              Windows
               0 File(s)              0 bytes
               3 Dir(s)  123,456,789,012 bytes free

This output is just a "string of characters" and is unstructured data from a program's perspective.


The Worldview of PowerShell: Everything is an "Object"

On the other hand, when you run a similar command, Get-ChildItem, in PowerShell, what's returned is a collection of structured "objects." An object is a block of data that contains various pieces of information (properties).

[Conceptual Diagram] One of the "file objects" returned by Get-ChildItem contains information stored in an orderly fashion, like this:

This is just like a well-organized table in Excel. Thanks to this structure, PowerShell makes flexible, data-based operations like "sort by size in descending order" or "display only files with a .txt extension" surprisingly easy.

PS C:\> Get-ChildItem

    Directory: C:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        7/7/2024   9:00 AM                Program Files
d-----        7/7/2024   9:05 AM                Users
d-----        7/7/2024   9:10 AM                Windows

Although they look similar, the most important point is that the quality of the data being handled behind the scenes is completely different.


Practical Comparison: Let's Compare with a Specific Task

Let's compare the two with a slightly complex task: "From the list of currently running services, display only those with 'Windows' in the name, sorted by name."

The Challenge in Command Prompt (cmd)

Trying to achieve this in cmd is very difficult. You can get a list of services with net start, but the result is just text. Extracting lines containing a specific word and then sorting them would require a combination of findstr, sort, and a complex for loop, which is nearly impossible for a beginner.

C:\> net start
These Windows services are started:

   Application Host Helper Service
   Background Tasks Infrastructure Service
   Base Filtering Engine
   ...
   Windows Audio
   Windows Connection Manager
   Windows Defender Antivirus Service
   ...

Accurately extracting only the information you want from this wall of text is a formidable task.


The Elegant Solution in PowerShell

In PowerShell, thanks to objects and the pipeline, you can write this process intuitively in a single line.

Get-Service | Where-Object { $_.DisplayName -like "*Windows*" } | Sort-Object -Property DisplayName

This one line performs the following steps in sequence:

  1. Get-Service: Gets all services as "objects."
  2. |: Passes the collection of objects to the next command.
  3. Where-Object ...: Filters the passed objects, selecting only those whose DisplayName property contains "Windows."
  4. |: Passes the filtered results to the next command.
  5. Sort-Object ...: Sorts the passed objects by their DisplayName property.

This comparison should have given you a real sense of PowerShell's superiority in data processing and its benefits.


Differences in Commands and Syntax

For compatibility with cmd, PowerShell allows you to use some old commands as "aliases." However, its native cmdlets and syntax are designed to be more modern and consistent.

Handling Variables

cmd: set variable_name=value

set MYVAR=Hello World

PowerShell: $variable_name = value

$myVar = "Hello World"

In PowerShell, the rule is to prefix variable names with a $.


Conditional Branching (if statement)

cmd:

if "%MYVAR%"=="Hello World" (echo Same)

PowerShell:

if ($myVar -eq "Hello World") { Write-Host "Same" }

PowerShell uses more intuitive comparison operators like -eq (equal) and -gt (greater than).


Is cmd Obsolete? The Future of the Windows Shell

You might be wondering, "If PowerShell is so convenient, is Command Prompt no longer needed?" The answer is, "No, it won't disappear anytime soon. However, you should do everything new in PowerShell."

Command Prompt is still included in Windows to maintain compatibility with very old systems and the vast number of batch files (.bat) created in the past. It's unlikely that Microsoft will completely discontinue it.

However, as you can see from the fact that PowerShell is the default setting in the standard Windows Terminal tool, there is no doubt that Microsoft has positioned PowerShell as the center of future Windows administration and automation.

If you're learning the command line from scratch, choosing PowerShell is undoubtedly the wise choice.


Conclusion

This time, we compared the decisive differences between PowerShell and Command Prompt (cmd), using diagrams (or the idea of them).

Now, when you face the black screen on Windows, you should have a clear reason why you should choose the "multi-functional tool" that is PowerShell. This concludes our PowerShell introduction series, but your journey into automation has just begun. We encourage you to incorporate PowerShell into your daily tasks and experience its power for yourself!