PowerShell 7 - Essentials
Hello koalas,
This article is about the essentials things to know about PowerShell 7. I hope you'll enjoy reading it.
Table of Contents
- History of PowerShell
- Why PowerShell is important to know?
- What are the differences between PowerShell 5.1 and PowerShell 7?
- Terminology
- Naming convention
- Basic commands
- Check PowerShell Version ($PSVersionTable)
- Launch a PowerShell 5.1 session
- Launch a PowerShell 7 session
- Open the official PowerShell documentation website from Microsoft
- Get current credentials (whoami)
- Write text to a file (echo >)
- Append text to a file (echo >>)
- Clear the screen of the shell, the terminal (cls)
- Create a directory, a folder (mkdir)
- Delete, remove a directory, a folder (rmdir)
- Run an application from the shell, terminal
- Calculate with PowerShell
- Get-Command
- Get-Command -CommandType
- List processes (ps, Get-Process)
- Show-Command
History of PowerShell
Before to be named as PowerShell, it was called "Monad".
PowerShell has been built at the top of .NET Framework.
PowerShell is an object-oriented/centered language. It works with objects and pipeline.
The scripting language is interactive. You can use the autocompletion system to help you writing faster and more efficiently your code.
Year | Versioning | OS integrated |
2006 | PowerShell 1.0 | Windows XP SP2, Windows Server 2003 SP1, Windows Vista |
2009 | PowerShell 2.0 | Windows 7, Windows Server 2008 R2, Windows XP SP3, Windows 2003 SP2, Windows Vista SP1 |
2012 | PowerShell 3.0 | Windows 8, Windows Server 2012, Windows 7 SP1, Windows Server 2008 SP1, Windows Server 2008 R2 SP1 |
2013 | PowerShell 4.0 | Windows 8.1, Windows Server 2012 R2, Windows 7 SP1, Windows Server 2008 R2 SP1, Windows Server 2012 |
2016 | PowerShell 5.0 / 5.1 | Windows 10 Build 1607, Windows Server 2016, Windows 7, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2 |
2018-2019 | PowerShell Core 6 / 6.1 / 6.2 | Windows 10, Windows Server 2019, Linux, MacOS |
2020 | PowerShell 7 | Windows 10, Windows Server 2019, Linux, MacOS |
Why PowerShell is important to know?
- PowerShell can manage anything from anywhere and is multiplatform (Windows, Linux, Mac)
- PowerShell can manage local systems and services
- PowerShell can connect to the cloud (ex: Microsoft Azure, Amazon AWS)
- PowerShell is a powerful automation language
- PowerShell is consistency
- PowerShell provides documentation (ex: Get-Help)
- PowerShell can import modules (extensions)
What are the differences between PowerShell 5.1 and PowerShell 7?
PowerShell 5.1 | PowerShell 7 |
Proprietary | Open Source |
Windows platforms only | Windows, Linux, MacOS |
Ships with Windows | Manual install |
PowerShell 5.1 is feature complete (no more development) | Active development (the one to use) |
PowerShell ISE | Visual Studio Code |
powershell.exe | pwsh.exe |
As you can see above, PowerShell 7 is the one to learn and go ahead with.
PowerShell 7 has the big advantage to be able to work on Linux and MacOS as well.
PowerShell 7 also can load modules (commands) from PowerShell 5.1 if some commands are missing with the latest version.
Important: PowerShell Core 7 can be installed along Microsoft PowerShell 5.1. Both versions can work at the same time on the same machine.
Terminology
Cmdlet | A cmdlet (pronounce command-let) is kind of "tiny" command. A cmdlet is a compiled code. A cmdlet does one thing (same as a Linux command). |
Function | A function behaves just like a cmdlet. |
Script | A script is a PowerShell "batch" file with a .ps1 extension. A script runs cmdlets and functions in a structured format. A script can be used to automate task sequences. |
Alias | An alias is an alternate name for a cmdlet, function, or script. It simplifies typing (usually shorter). An alias allows users to make the transition from bash (cmd), Linux, or even MacOS. It's an aid for new comers. |
Parameter | A parameter is used by all command types. A parameter customizes the behavior of the command. A parameter could be "positional". It means it has to be in a specific position to be accepted by the command. A parameter syntax is always: -ParameterName Value(,Value2, Value3,...). |
Naming convention
PowerShell has a naming convention: Verb-Noun (always a dash (-) between verb and noun)
The verbs are from the .NET Standard. You can see the full list with the command:
Get-Verb
The nouns are singular version of the word you use. It also can be prefixed to facilitate the understanding.
Get-WinEvent
Get-WindowsUpdate
Basic commands
Check PowerShell Version ($PSVersionTable)
Command | $PSVersionTable |
Description | Shows information about the version of PowerShell. |
Example | $PSVersionTable |
Output |
Launch a PowerShell 5.1 session
Command | powershell.exe |
Description | Launches a new Powershell 5.1 session. |
Example | powershell.exe |
Output |
Launch a PowerShell 7 session
Command | pwsh |
Description | Launches a new PowerShell 7 session. |
Example | pwsh $PSVersionTable |
Output |
Open the official PowerShell documentation website from Microsoft
Command | start https://aka.ms/powershell |
Description | Goes to the official PowerShell documentation from Microsoft. |
Example | start https://aka.ms/powershell |
Output |
Get current credentials (whoami)
Command | whoami |
Description | Check the current credentials used. |
Example | whoami |
Output | YourComputerName\username |
Write text to a file (echo >)
Command | echo "Your text" > filePath |
Description | Write text to a file. |
Example | echo "First line" > MyTextFile.txt |
Output |
Append text to a file (echo >>)
Command | echo "Your text" >> filePath |
Description | Append text to a file. |
Example | echo "Second line" >> MyTextFile.txt |
Output |
Clear the screen of the shell, the terminal (cls)
Command | cls or Clear-Host |
Description | Clear the screen (of your shell/terminal). |
Example | cls Clear-Host |
Output |
Create a directory, a folder (mkdir)
Command | mkdir, md, or New-Item -ItemType Directory |
Description | Create a directory, a folder. |
Example | mkdir "YourFolderName1" md "YourFolderName2" New-Item -ItemType Directory -Name "YourFolderName3" |
Output |
Delete, remove a directory, a folder (rmdir)
Command | del, Remote-Item -Path "YourFolderName" |
Description | Delete a directory, a folder. |
Example | del "YourFolderName1" Remove-Item -Path "YourFolderName2" rmdir "YourFolderName3" |
Output | Nothing to show because it has been deleted. |
Run an application from the shell, terminal
Command | notepad, calc, pwsh, powershell, and so on |
Description | Run an application from the shell, the terminal. |
Example | notepad calc pwsh powershell ... |
Output | The application you've run. |
Calculate with PowerShell
Command | A calculation |
Description | Calculate what you have typed. |
Example | 2*2 (3*4)-2 |
Output | 4 10 |
Get-Command
Command | Get-Command |
Description | Get all commands on your system. |
Example | Get-command -Verb Get -Noun win* |
Output |
Get-Command -CommandType
Command | Get-Command -CommandType <type> |
Description | Get the type of a command. |
Example | Get-Command -CommandType Cmdlet Get-Command -CommandType Function Get-Command -CommandType Script Get-Command -CommandType Alias Get-Command -CommandType Application |
Output | List of commands with the <type> you choose. |
List processes (ps, Get-Process)
Command | ps or Get-Process |
Description | List processes on your computer. |
Example | ps *explorer* Get-Process *explorer* |
Output |
Show-Command
Command | Show-Command |
Description | Show a Window with all the commands present on your system. You also can use fields to create your command and then use the "Run" or "Copy" button at the bottom of the Window. |
Example | Show-Command |
Output |
That's it for this article! I hope you enjoyed reading it.
Keep learning,
Didier