Introduction
PowerShell is a cross-platform CLI tool that replaced the Command Prompt (CMD) in Windows as of Windows 10 build 14971. While the Command Prompt remains an option within the operating system, PowerShell aims to improve the CLI experience in Windows by introducing improved scripting support and pipelines.
This article provides a detailed comparison of PowerShell and CMD and advises when to use each tool.
PowerShell vs. CMD: Overview
CMD and PowerShell both aim to provide command-line access to a Windows system. However, they significantly differ in complexity and the features they offer. Below is an overview of the most critical points of comparison.
PowerShell | CMD | |
---|---|---|
First Release | 2006 | 1987 |
Cross-Platform Compatibility | Windows, macOS, Linux | Windows |
Language | Complex. Features cmdlets and command aliases | Simple. MS-DOS-based |
Redirection Capabilities | Full range of redirection and piping capabilities | Basic input and output redirection |
Output | Objects | Plain text |
Scripting | .NET-framework-based (PS1 files), full-featured ISE | Batch scripting (BAT or CMD files), no ISE |
Libraries | Full access to .NET libraries | No access |
WMI Integration | Full | Limited |
MS Online Connectivity | Built-in | No |
Program Support | Extensive | Limited |
Help | Via the Get-Help cmdlet |
Via the help and /? commands |
Windows PowerShell vs. CMD: Comparison
The comparison table shows that CMD and PowerShell are two shell interpreters that serve significantly different use cases. The sections below provide an analysis of each of the comparison points.
Maturity
CMD has been part of the Windows ecosystem since the earliest releases of the operating system. With the first version released in 1987, it has reached the level of maturity which guarantees stability and reliability. However, the development of new features for the Command Prompt has been stagnant in the past two decades, making it less suitable to respond to the needs of modern system administrators.
PowerShell is a modern tool that is being actively developed and improved. Although much younger than CMD, it is mature enough to be a reliable system administration solution.
Cross-Platform Usability
CMD was developed for multiple operating systems, such as Windows NT, Windows CE, OS/2, and eComStation. However, it does not support modern OSs other than Windows.
PowerShell‘s initial releases were Windows-only. However, as of version 6, PowerShell also supports macOS and Linux. Cross-platform compatibility aims not to replace the native macOS and Linux shell experience but to facilitate collaboration in mixed-environment teams.
Below is PowerShell in the Ubuntu terminal:
Language
CMD features a simple MS-DOS command syntax. Parameters, arguments, or both can follow the commands.
PowerShell‘s language relies on cmdlets (pronounced command-lets), commands that typically have a Verb+Noun structure. Below is the list of some frequently used cmdlets:
Get-Service
. Show information about services on the system.Get-Process
. Show the processes currently running on the system.Get-Childitem
. List subdirectories and files in a directory.Get-Item
. See information about a directory or a file.Set-ItemProperty
. Set file, directory, or registry key properties.
PowerShell also features aliases, i.e., alternative names for commands that are easier to remember and simpler to type. For example, you can execute the Get-Childitem
cmdlet by typing the ls alias.
Redirection Capabilities
While it supports basic output redirection, such as reading and writing to a file, CMD does not offer the redirection capabilities expected from a modern shell interpreter. The following are the redirection commands available in CMD:
- (>) creates a new file or overwrites an existing file with the standard output.
- (>>) appends the standard output to a file.
- (<) redirects the file content to a command.
PowerShell features more extensive redirection capabilities. The most important addition is the ability to pipe the command output to another command. For example, view only the list of Microsoft processes on the system using PowerShell by typing:
Get-Process | Where-Object {$_.Name -like "Microsoft*"}
Below is the list of PowerShell commands for redirection:
- (>) creates or overwrites a file with standard output.
- (>>) appends standard output.
Get-Content
cmdlet replaces (<) for redirecting file content.- (2>) redirects stderr (error) output to a file.
- (2>&1) combines standard and error output.
- (|) pipes the output of one command to another.
Output
CMD shows only plain text as the command output. Since the output can be formatted that way only, processing structured data with CMD is challenging.
On the other hand, PowerShell outputs objects that can be arranged in multiple ways – as tables, lists, and CSV files. For example, you can show the contents of the directory as a list by piping the output to the Format-List
cmdlet:
ls | Format-List
You can also choose Format-Table
to show a table view or Export-Csv
to create a CSV file containing the command output.
Scripting
The CMD scripting language relies on BAT and CMD files written in Notepad. Batch scripting lacks many modern features that administrators frequently use today, such as support for advanced data structures (arrays, dictionaries, objects, etc.), control structures, and functions.
PowerShell offers an extensible object-oriented scripting language based on the .NET framework, with a full-featured ISE, advanced control structures, and error handling. It allows administrators to automate complex tasks like API interaction and communication with .NET libraries.
Libraries
CMD has no access to libraries and modules, which limits its usefulness in task automation. On the other hand, PowerShell has full access to .NET libraries that support a wide range of functionalities, such as working with databases, web applications and services, and XML.
WMI Integration
CMD has a limited integration with Windows Management Instrumentation (WMI) infrastructure. The wmic
tool returns WMI information formatted as text, without advanced management features, the ability to create or modify classes, or manage event subscriptions.
PowerShell offers direct access to WMI with the Get-WmiObject
cmdlet. Since the shell outputs objects, administrators can manipulate data and automate WMI-related tasks. PowerShell also enables users to create WMI scripts and query WMI using WMI Query Language (WQL).
Microsoft Online Connectivity
CMD does not integrate with Microsoft online services natively and provides no direct support to cloud-based resources. Since it does not support modules, it relies on third-party scripts to connect to services such as Azure or Microsoft 365.
PowerShell offers official modules for seamless integration with Microsoft services. It provides direct integration with Azure and Microsoft 365 services via dedicated cmdlets. For example, the Connect-AzAccount
cmdlet allows users to log into their Azure account directly from PowerShell.
Supported Programs
CMD supports native Windows executables. While some third-party tools can be executed using CMD, the integration is limited by CMD’s limitations, such as text-based output, limited API interaction, and basic scripting capabilities.
PowerShell supports an extensive list of programs, both Windows-native and third-party. It allows developers to create custom cmdlets to provide shell integration so the administrators can tailor their scripts according to their needs.
Help
CMD offers help
and /?
commands to show help. While they can provide quick assistance for the command syntax, these commands are basic in functionality and offer limited information.
PowerShell has a more comprehensive help system offered through the Get-Help
cmdlet. It provides a short synopsis, command syntax, description, and examples for each PowerShell cmdlet.
When to Use PowerShell or Command Prompt?
Given its simplicity and maturity, use CMD to:
- Execute quick and straightforward commands.
- Run third-party command-line tools designed for CMD.
- Run legacy batch scripts.
The modern design and advanced capabilities of PowerShell make it the better choice for:
- Task automation and system configuration management.
- User and event administration.
- Cloud resource management.
- Data processing.
Conclusion
This article compared PowerShell and CMD, two tools for managing Windows systems via the command line. It also provided tips when using one of the tools has benefits.
If you feel comfortable using the CLI in Windows, you should learn how to install winget (Windows Package Manager).
Đăng ký liền tay Nhận Ngay Bài Mới
Subscribe ngay
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !
Add Comment