PowerShell is a powerful scripting language and command-line shell designed to simplify the management of systems. It stands out due to its unique features, such as object-based pipelines and deep integration with the .NET Framework.
Type
$PSVersionTable
to check your version of PowerShell.
Structured commands
PowerShell commands, known as cmdlets, follow a consistent Verb-Noun naming convention:
Get-Process
: Retrieves information about processes running on a systemSet-Location
: Changes the current working directoryGet-Command *process*
: Finds out which PowerShell commands (and Windows applications) contain the word processGet-Help Get-Process
: Finds out what a command such as Get-Process does
However, PowerShell has built-in aliases for many common Linux commands to ease the transition for users familiar with Unix-like systems. These aliases map to equivalent PowerShell cmdlets. Just type Get-Alias
to get a list of all aliases.
Common Linux commands like ls
, cd
, clear
, cp
, rm
, echo
, and cat
are typically aliased to their PowerShell equivalents by default.
Cmdlets, functions and applications
- A cmdlet is a native PowerShell command-line utility. These exist only inside PowerShell and are written in a .NET Core language such as C#.
- A function can be similar to a cmdlet, but rather than being written in a .NET language, functions are written in PowerShell’s own scripting language.
- An application is any kind of external executable, including command-line utilities such as
ping
andipconfig
.
Deep integration of objects
Unlike traditional text-based shells, PowerShell works with objects. This means that the output of a command is not just plain text but a rich object that can be further manipulated.
In this snippet, the string “Hello World” is treated as a fully functional object from the .NET Framework. It provides the Get-Member
cmdlet to explore the properties and methods of these objects:
Navigating the system with providers
PowerShell also offers providers that allow you to navigate various data stores, such as the file system, registry, and more. For example, to navigate the Windows registry:
A PowerShell provider, or PSProvider, is an adapter. It’s designed to take some kind of data storage, and make it look like a disk drive. You can see a list of installed PowerShell providers right within the shell:
You use a provider to create a PSDrive. A PSDrive uses a single provider to connect to data storage.
Understanding how the filesystem is organized
PowerShell and Linux have some differences in how they handle and refer to the filesystem. Because a PSDrive might not point to a filesystem, PowerShell doesn’t use the terms file and folder. Instead, it refers to these objects by the more generic term item. Items can, and often do, have properties. Knowing those facts should help you make sense of the verbs and nouns in the command list we showed you earlier.
Finding and installing modules
Modules are self-contained units designed for easy distribution. Microsoft has introduced a module called PowerShellGet, which provides similar functionality to popular package managers in Linux such as rpm, yum, and apt-get. The central repository for PowerShell content is the PowerShell Gallery, where users can discover, install, update, and publish PowerShell packages. The PowerShellGet module includes cmdlets to facilitate these actions with packages from the PowerShell Gallery.
- Terminal-Icons: Adds file and folder icons to the PowerShell terminal, enhancing visual feedback
- posh-git: Enhances PowerShell’s Git integration, showing Git status in the prompt and providing Git command auto-completion
- ImportExcel: Import/export Excel spreadsheets, without Excel
PowerShell profiles
PowerShell profiles are scripts that can be used to customize the PowerShell environment when the shell starts. A PowerShell hosting application, such as the console or VS Code, allows users to send commands to the PowerShell engine, which executes the commands and displays the results. The hosting application is responsible for loading and running profile scripts each time the shell starts.
Profile scripts can be used to perform various customizations, such as loading modules, changing the starting directory, or defining functions.
Details are available if you run help about_profiles
, but you mainly need to consider whether you’ll be working in multiple different hosting applications. For example, we tend to switch back and forth between Windows Terminal, and VS Code. We like to have the same profile running for both.
Because we want the same shell extensions to load whether we’re using the console host or the VS Code, it’s recommended to customize $home\Documents\WindowsPowerShell\ profile.ps1
.