Tutorials

Powershell: what it is and basic and 【recommended comandos commands

Table of contents:

Anonim

Just as it works and fulfilling tasks similar to those that can be obtained with the command prompt, the native Windows PowerShell tool is an input resource for the Windows operating system. A tool that is often used for its flexibility by both engineers and system analysts on a daily basis in their jobs.

Whether you have to manage servers or systems, PowerShell is an improved and advanced version of the traditional Command Prompt. The tasks and functions of Windows Powershell are basically the same as those found in the CMD, (sending commands to Windows through specific commands), although it also has several extra functions that can be very useful for the user.

Contrary to what happens in the CMD, PowerShell presents us with a robust scripting interface with specific functions where you can run them to carry out various processes under the Windows system. With such an interactive command line, you can launch commands to automate different tasks.

This tool was already compatible with Windows XP, but in order to use it, it had to be downloaded and installed. Now with Windows 10 the Powershell is already pre-installed and with significant visibility to make access easy.

Index of contents

What is Windows Powershell?

The PowerShell interface can be used for different purposes, such as running advanced applications or even simpler tasks such as knowing the current time. Also, Powershell commands can work together, combining on the command line for more specific and advanced results. This has been called " pipelining".

Meanwhile, Powershell also offers an option that can come in handy for some users: the ability to add other commands created by the same users to the console.

Even though PowerShell was created several years ago and with Windows 10 it has greater visibility and more prominence, it is still a little-known tool, not only for basic users, but also for many computer operators who do not know all the advantages they can offer. the cmdlets (script light) of this console.

Due to all the advantages that these commands can offer, and taking into account that Microsoft has been dedicating more space to Powershell so that Windows users get used to it, it is necessary to begin to know more about Powershell functions, what they are used for and how they can benefit us by using them.

How to open Windows PowerShell

The PowerShell tool can be opened quickly by accessing the Run function included in Windows.

  • To do this, simultaneously press the Windows + R keys. In the Run box that just opened, type "PowerShell" and click OK or directly press the Enter key.

Another option you have to access Powershell is to use the search engine offered by Cortana, located in the lower left part of the screen, from where you can search for this tool.

Essential PowerShell Commands (Tutorial)

In PowerShell, commands are called "cmdlet, " and you can use the tab key to have Powershell auto-complete the name of the cmdlet you want to use.

Windows PowerShell was created with its backward compatibility in mind, making it a resource that works well with the same commands that the CMD uses. Knowing this, the same commands that were used in the Command Prompt can be used, but in a more advanced interface and with many more commands.

Here we have put together a significant number of useful cmdlets that can be used in Powershell and detail the syntax of each one, as well as the specific function of each one.

To start with the basics, and take a quick look at the cmdlets that PowerShell offers us, we can execute the command "Show-Command", through which a window will open, showing us an extensive and complete list of all available commands.

Get-Command

In case you want to know all the cmdlets that PowerShell offers, you can do it by typing this command in the console.

Windows PowerShell allows, through this command, to know all the functions and characteristics that its cmdlets include, presented in the form of a list that describes the functions of each one, as well as their special parameters and options.

To obtain this list of commands, it is necessary to write "Get-Command" followed by a specific parameter, with which information will be obtained from the cmdlet in question. For example, if we write in Powershell "Get-Command * -help *", we will see a series of commands that accept the parameter "-help".

If you add an asterisk to each side of the parameter, as we have done in the example, you will get all the possible combinations that the Get-Command cmdlet uses when accompanied by “-help”.

Typing “Get-Command -Name in the console ”We get a set of commands that include that specific name. It may happen that you do not remember or do not know the correct name of a cmdlet. In this situation, you can include the two asterisks on each side of the name as mentioned above, for example, "Get-Command -Name * set *", so you could see a list of cmdlets that include the term "set" in your name.

Get-Host

Executing this command gives you the version of Windows PowerShell that the system is using.

Get-History

This command provides a history of all the commands that were executed under a PowerShell session and that are currently running.

Get-Random

Executing this command produces a random number between 0 and 2, 147, 483, 646.

Get-Service

In certain occasions, it will be necessary to know which services were installed in the system, for which the Get-Service command can be used, which will provide information about the services that are running and those that have already been stopped.

To use this cmdlet, enter "Get-Service" in the console, while using any of the additional parameters, in a syntax similar to the following example:

Get-Service | Where-Object {$ _. Status -eq "Running"}

With this, the services are executed in the system. In case this command is executed without any parameter, a list of all the services with their respective states will be presented (“Running or“ Stopped ”, for example).

If you already know exactly which command you want to get information about, using Get-Service is much more practical than going to the Windows Control Panel and working from the Windows GUI (graphical user interface).

Get-Help

Especially very useful for novice users of Powershell, this command presents a basic help to learn more about cmdlets and their functions.

In case you are using PowerShell for a short time, it is very likely that you will find yourself disoriented and with some difficulties; In these circumstances, Get-Help will become your guide, since this command provides the essential documentation about cmdlets, functions, commands and scripts.

In the same way, its use is not complicated at all: you just have to write "Get-Help" accompanied by the cmdlet of which you want to know more details. To exemplify its use, we could be looking for more information from the "Get-Process" cmdlet, in which case it would be enough to write "Get-Help Get-Process".

To have a clearer idea about how Get-Help works in Windows PowerShell, just by running this command we will see a description along with a brief explanation on how to use it.

Get-Date

To quickly know what day it was on a certain date in the past, using this command you will get the exact day. For example, to know what day was May 20, 2009, you would have to write in Powershell:

"Get-Date 05.05.2009", entering the date in "dd.mm.aa" format. In case of executing Get-Date alone, it will give us the current date and time.

PS C: \ Users \ MiguePR> Get-Date Saturday, July 27, 2019 12:00:40

Copy-Item

With this command you can copy folders or files.

If you are looking to make a copy of files and directories on your storage drive, or if you need to copy keys or registry entries, Copy-Item is the right cmdlet. It works very similar to the command "cp" that is included in the Command Prompt, although it is much better.

For this, the Copy-Item command must be used to copy and modify the name of elements using the same command, with which a new name can be established for said element. In case you want to copy and rename the file "ProfesionalReview.htm" to "Proyectitosbuenos.txt", write:

Copy-Item "C: \ Proyectos.htm" -Destination "C: \ MyData \ Proyectos.txt".

Invoke-Command

When you want to run a script or PowerShell command (locally or remotely, on one or more computers), "Invoke-Command" is going to be your best option. It is simple to use and will help you manage batch computers.

You need to type Invoke-Command next to the script or command with its exact location.

Invoke-Expression

With Invoke-Expression another expression or command is executed. If you find yourself entering an input string or an expression, this command will first parse it and then execute it. Without this command, the string returns no action. Invoke-Expression only works locally, unlike Invoke-Command.

To use this command, Invoke-Expression must be written together with an expression or command. For example, you could set a variable "$ Command" with a command that points to the "Get-Process" cmdlet. By executing the command "Invoke-Expression $ Command", "Get-Process" will act in the same way as a cmdlet on the local computer.

Similarly, a function can be executed in a script with the use of a variable, which is very useful when working with dynamic scripts.

Invoke-WebRequest

Through this cmdlet, similar to cURL in Linux, you can do a login, a scraping and the download of information related to services and web pages, while working from the PowerShell interface, monitoring some website of the you want to get this information.

To carry out these tasks, it must be used as Invoke-WebRequest along with its parameters. With this, it is possible to get the links that a specific website has with the following example syntax:

(Invoke-WebRequest –Uri 'https://wwww.ebay.com'). Links

In this case, the links from the eBay site would be obtained.

Set-ExecutionPolicy

While we can create and start scripts (.ps1) from PowerShell, we are limited due to security concerns. However, this can be modified through the security category using the Set-ExecutionPolicy cmdlet.

You only need to type Set-ExecutionPolicy next to one of the four security options to make the required changes:

  • RestrictedAll SignedRemote SignedUnrestricted

For example, if we want to set the level of security restricted, we would have to use:

Set-ExecutionPolicy -ExecutionPolicy Restricted

Get-Item

In case you are looking for information about an item with a specific location, such as a directory on the hard drive, the Get-Item command is the one indicated for this task.

It should be clarified that the content of the element itself, such as subdirectories and files in a specific folder, is not obtained, unless you explicitly request it.

On the other side of Get-Item we find the Remove-Item cmdlet, which allows the specified item to be removed.

Remove-Item

In case you want to delete items like folders, files, functions and variables and registry keys, Remove-Item will be the best cmdlet. The important thing is that it offers parameters for entering and ejecting elements.

With the Remove-Item cmdlet you can remove items from specific locations with the use of certain parameters. As an example, it is possible to remove the file "Finanzas.txt" using the following command:

Remove-Item "C: \ MyData \ Finance.txt"

Get-Content

When you need everything that a text file contains in terms of content in a specific path, open it and read it using a text editor such as Notepad. Using Windows PowerShell, you can use the Get-Content command to browse what a file contains without opening it.

For example, it is possible to obtain 20 lines of text included in the file "Proyectos.htm", for which you can write:

Get-Content "C: \ Proyectos.htm" -TotalCount 20

This cmdlet is similar to the previous Get-Item cmdlet, but with which we can obtain what is included in the file that you have indicated. If you run this command for a file with a txt extension, it will fully reveal the text included in that file. If you use it in a png image file, you're going to get a lot of meaningless and unreadable binary data.

If used alone, Get-Content is not very useful. But it can be mixed with more specific cmdlets in order to obtain more precise results.

Set-Content

With this cmdlet it is possible to store text in a file, something similar to what can be done with “echo” in the Bash. If used in combination with the Get-Content cmdlet, you can first see what is contained in a certain file and then make the copy to another file through Set-Content.

For example, you can use the Set-Content cmdlet to add or replace what is contained in a file with other content. Finally, it can be combined with the aforementioned command to save it with a new name (example.txt) as follows:

Get-Content "C: \ Proyectos.htm" -TotalCount 30 | Set-Content "Example.txt"

Get-Variable

If you are in PowerShell trying to use variables, this can be done with the Get-Variable cmdlet, with which you will be able to view these values. This command displays the values ​​in a table, from which wildcards can be used, included, and excluded.

To use it you just have to write "Get-Variable" accompanied by its parameters and other options. For example, if you would like to know the value of the variable "discount" write the following:

Get-Variable -Name "discount"

Set-Variable

The value of a variable can be set, modified or reinitialized with this cmdlet. To set the value of the variable of the previous case, the following should be written:

Set-Variable -Name "discount" -Value "Value is set here"

Get-Process

Often, we use Task Manager in order to discover exactly what processes are running on our PC. In PowerShell, any user can know this by running this cmdlet, with which they will get the list of currently active processes.

The Get-Process cmdlet bears some resemblance to Get-Service, although in this case it provides information about the processes.

Start-Process

With this cmdlet, Windows PowerShell makes it much easier to run processes on the computer.

For example, if you need to use the calculator, you can open it quickly and easily by typing the following:

Start-Process -FilePath “calc” –Verb

Stop-Process

With this cmdlet you can stop a process, whether it was started by you or by another user.

Continuing with the example of the Calculator, if you want to completely interrupt its running processes, write the following in PowerShell:

Stop-Process -Name "calc"

Start-Service

If you need to start a service on the PC, the Start-Service cmdlet is the one indicated in this case, serving the same way even if said service is disabled on the PC.

To start the Windows Search service, this syntax is used:

Start-Service -Name "WSearch"

Stop-Service

With this command you stop the services that are running on the computer.

Stop-Service -Name "Wsearch"

With this order you will stop the "Windows Search" service.

Exit

You can exit PowerShell using the Exit command.

Conclusion about Windows PowerShell

It may seem that some of these commands are of little use, although that happens because they are commands that do not stand out until other parameters are entered that effectively make the PowerShell show its full power.

These parameters come in handy when you're writing your own Cmdlets or Light Scripts, which is a good way to get the most benefit from PowerShell functions.

Although they are commands that you use sporadically, knowing how they work and what they are for has its advantages, considering that Microsoft is currently focused on PowerShell more than ever before.

At the end of the day, cmdlets are essential commands that you should know if you want to get the most out of Windows PowerShell.

Tutorials

Editor's choice

Back to top button