Windows PowerShell and the command prompt are both Microsoft command line applications. They can be used separately due to their integration into Windows systems or in the free and open source application, Windows Terminal, from where they can be run natively.
Equivalent to the Linux terminal or that provided by other operating systems such as macOS, the two are visually quite similar, but in practice they offer significant differences. Windows PowerShell is more intended for system administrators and advanced users who want to perform tasks in the operating system more flexibly and quickly.
And it is that Windows console is still needed. Despite the graphical nature of Windows and its almost zero dependence on these consoles, its use offers significant advantages in certain tasks. Although they run in text mode and are not as intuitive as a graphical user interface, they show their potential when executing repetitive tasks, sometimes where the graphical interface is blocked, to manage certain components or access certain system information that It is not available any other way.
Its operation is simple as well as powerful. We write the command and the CMD application acts as an interpreter for its execution. The use of modifiers for each of the commands allows hundreds of combinations to be executed for a wide variety of tasks in its two forms of use: user and administrator mode.
Windows PowerShell
Microsoft began working at the beginning of the century on a console that was more advanced than its previous command lines. Known in its development as Microsoft Shell or “Monad”, it was released integrated into Windows Vista as PowerShell.
A console interface (CLI) that, in addition to the command prompt capabilities, offers a greater number of possibilities as it allows you to create your own commands and scripts using the C# programming language. Both PowerShell and C# are built into the Microsoft .NET Framework, which means you have access to many pre-existing features and tools to help you create better commands and scripts with less effort.
Windows PowerShell offers many advanced features such as remote task execution, background tasks, task automation, command piping, and more, making it the preferred choice for system administrators and more advanced users. Logically, the learning curve is higher than the command line interpreter.
Microsoft has been improving the functions of PowerShell in the successive releases of its operating system, but it has been in Windows 10 where it has reached its peak. maximum performance with an improved package manager over what the previous OneGet offered and that allows you to manage all the software you download, install and delete.
Also interesting is the added native support for OpenSSH, the set of applications that allow encrypted communications over a network using the SSH protocol, which means that developers do not have to depend on third-party software (such as the popular PuTTY) every time. that connect to a remote server, and can be done from this Windows console.
Another important issue of Windows PowerShell is the availability on GitHub under the MIT license and with added versions for Mac and Linux, after its release as open source. The objective of this movement was to facilitate the implementation of Microsoft technologies on Linux servers with two user profiles in mind, the Linux system administrators themselves and those of Windows who are looking for a more gradual transition to the free system.
Like the command prompt, accessing Windows PowerShell is very easy using the “Windows + X” hotkeys. You can place one or the other in the advanced start menu directly on the taskbar with an option in settings.
As you may have read, the Windows console “also exists” and is increasingly powerful especially if you use the advanced version Windows Powershell. Although some of its functions can be executed from the graphical interface in a simpler and more intuitive way, average and advanced users with some knowledge can obtain great results. And as an example of use, we leave you with the essential commands to use in the console.
Ten essential commands to get started in Windows PowerShell
Get-Command
If you are taking your first steps in PowerShell, we are sure that you will be grateful to have a cmdlet as useful as “Get-Command”. When you run it, Windows will show you all the commands available for your current session in PowerShell.
get-command
Get-Help
If we need help, all we have to do is ask for it. And here the Get-Help command will provide us, never better said, with fundamental help when we do not know exactly what we are doing or what other options we can work with on our command line. A common way to use this cmdlet is as follows:
Get-Help ((-Name) ) (-Path ) (-Category <String()>) (-Component <String()>)
Set-ExecutionPoliciy
As a security measure and to prevent the injection of malicious code, Microsoft disables by default the ability to execute scripts in the PowerShell environment. Most developers, however, are (very) interested in being able to have this functionality.
To achieve this, the Set-ExecutionPolicy command establishes different levels of control, around which different scripts can be executed. They are the following:
-
- Restricted: It does not load configuration files or run scripts. This is the default setting.
- Allsigned: Requires all scripts and configuration files to be signed by a trusted publisher.
- Remotesigned:Requires all scripts and configuration files downloaded from the Internet to be signed by a trusted publisher
Unrestricted: Load all configuration files and run all scripts
As in the case of get-command, if we do not know what environment “we are moving in” we can easily find out by executing the command Get-ExecutionPolicy
Get-Service
In addition to the commands we can work with, it is also useful to know what services we can count on in the system. We can find out simply with the Get-Service command. A typical output for this command could be the following:
Status Name DisplayName
------ ---- -----------
Running AdobeActiveFile... Adobe Active File Monitor V4
Stopped Alerter Alerter
Running ALG Application Layer Gateway Service
Stopped AppMgmt Application Management
Running ASChannel Local Communication Channel
If we need to know the status of a specific service, we will write its name after the suffix -Name
ConvertTo-HTML
If we need to extract the data from PowerShell to share it with a third party, the ConvertTo-HTML command is a highly recommended way to do it. Using it is as simple as including it as a suffix to any other command whose output we want to save. We must also determine the name of an HTML file. For example, in the following way:
PS C:> get-alias | convertto-html > aliases.htm
PS C:> invoke-item aliases.htm
If instead of HTML we prefer the CSV format, we can use the command in the same way Export-CSV
.
Get-EventLog
One of the most interesting uses of PowerShell is to analyze our team’s event logs. To do this we will use the Get-Eventlog cmdlet. To do this, we will use the -Log parameter followed by the log file name to view a specific log. One way to use it is the following:
Get-EventLog -Log "Application"
Get-Process
In the same way that we may be interested in accessing a list of commands and services, it can be very useful for us to understand what processes are running at a given time. The Get-Process command offers us this information.
In addition to knowing what is being executed, this cmdlet serves as the first step to terminate a process that is not working as it should or has been blocked. For this we will use the Stop-Process order. One way to perform both actions could be the following:
Get-Process
Stop-Process -processname notepad
Clear-History
Do we want to delete the history of commands that we have used until now? Nothing as easy as using the Clear-History command. Are we interested in deleting the history of a specific command? We will add the -Command appendix. For example, like this:
Clear-History -Command *help*
Where-Object
We come to one of the most important cmdlets that you have to know. Creates a filter that controls the objects to be passed with a command pipeline. Filters the objects it receives, either as piped input or through the “- inputobject” parameter. Determines which objects to pass through the pipeline by evaluating a script block that may include a reference to an object to be filtered. If the execution result is true, the object to be processed is passed through the pipeline, otherwise the object is discarded.
Set-AuthenticodeSignature
If we want to keep our work secure and avoid possible modifications, we can use the Set-AuthenticodeSignature cmdlet to add an Authenticode signature to a script or file.