Understanding the Use of PowerShell Command (with examples)
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and the associated scripting language. With the introduction of PowerShell Core (now just referred to as “PowerShell”), Microsoft has expanded PowerShell’s capabilities to be cross-platform, running on Windows, macOS, and Linux. The original “powershell” command pertained to the legacy Windows PowerShell. The newer, cross-platform version uses pwsh
to start the shell.
Use case 1: View the documentation for the command referring to the latest, cross-platform version of PowerShell (version 6 and above):
Code:
tldr pwsh
Motivation:
As PowerShell evolves into a cross-platform tool, many developers and systems administrators who work in heterogeneous environments (such as managing both Windows and Linux servers) need to be familiar with the latest version and its capabilities. The tldr
command is a valuable resource to quickly understand fundamental command usages. By focusing on pwsh
, users can prepare themselves to leverage the modern, cross-platform PowerShell effectively.
Explanation:
tldr
: This is a command-line tool that provides simplified, community-driven manual pages. It breaks down the essentials of a command in an easy-to-read format, focusing on practical examples.pwsh
: This refers to the cross-platform version of PowerShell. It’s used instead of the traditionalpowershell
command when referring to PowerShell Core versions 6 and above.
Example Output:
pwsh
Invoke-Command: Run a command on a remote machine.
Get-Command: Retrieve a list of all available cmdlets.
Get-Help: Display help for a cmdlet.
This output gives a concise look at some of the primary functions and usages of the PowerShell commands available in the cross-platform version, making it a swift guide for users to start experimenting with the new features in pwsh
.
Use case 2: View the documentation for the command referring to the legacy Windows PowerShell (version 5.1 and below):
Code:
tldr powershell -p windows
Motivation:
For those maintaining older systems or needing to work with environments where the newer version of PowerShell is not yet implemented, it’s crucial to understand the legacy Windows PowerShell features. Using the tldr
command with the legacy version allows users to quickly access and refer back to the traditional functionalities they might need to troubleshoot issues or develop scripts on older systems.
Explanation:
tldr
: As explained before, this tool is perfect for getting straightforward examples and explanations of command-line utility.powershell
: This refers to the legacy Windows PowerShell 5.1 and earlier. It is crucial for supporting commands and scripts that run on Windows environments which have not transitioned to the cross-platform version.-p windows
: This option specifies that the documentation should be catered for the Windows platform. Given the difference betweenpwsh
and the olderpowershell
, it ensures you receive the correct guidance tailored specifically to how the command functions within a Windows OS.
Example Output:
powershell
- Start a PowerShell session:
powershell
- Execute a command:
powershell -Command "Get-Process"
- Run a script:
powershell -File "script.ps1"
The output illustrates simple commands you might run using the legacy version of PowerShell, allowing the user to quickly structure similar commands in their systems or scripts without needing to consult more extensive documentation heedlessly.
Conclusion
Understanding both the modern pwsh
and the legacy powershell
commands is essential for anyone working across different environments and platforms. While transitioning to newer technologies is often desired, legacy systems remain critical within many organizations, and being adept at leveraging both versions of PowerShell ensures versatility and continuity in managing systems. The tldr
command enhances learning by providing concise usage examples, bridging knowledge gaps swiftly and effectively.