Windows

Powershell errors: learn to identify each of them

Once we have installed Windows 10 for the first time, we find a good number of applications and functions that are already included. All this will be useful to start working from the first moment, as well as to take full advantage of the operating system, as with Powershell.

Among these we find elements as important as the File Explorer, the Control Panel, the Settings application, or the Powershell. In addition to all this we must add the UWP applications that we find from the start. Well, in these lines we are going to focus on one of those important functions, the PowerShell. This is a much more powerful element than the Windows Command Prompt.

As many of you know, both are used based on a series of commands that allow us to use the operating system in a more effective and precise way. Of course, due to that power we are talking about, there is a greater possibility of errors. Therefore, if we are working on a script and it suddenly crashes, we may not know where to start looking. That is why we are going to teach you to identify the most common mistakes in this section.

Use Powershell to get the most out of a system like Windows

In principle this element is usually used by somewhat more advanced users who already know the basics and error handling here. However, there are some specific aspects of PowerShell that we must also know. The first thing to know when facing an error in PowerShell, is what stops the script from executing.

If the bug doesn’t affect the rest, the best we can do is tell PowerShell to ignore it and move on. On the other hand, it is not always clear when a ” cmdlet ” throws a completion error. Some of these “cmdlets” return an error but do not stop the script, which we could ignore. Of course, we must know that this is where the “-ErrorAction” parameter comes into play.

Say that this is used to force a “cmdlet” to handle the error as we want. To get it working, just add it to the end of the “cmdlet” line. This is a clear example of this: “Get-ChildItem -Path« ~ \ Documents \ *. Jpg »-Recurse -ErrorAction Stop”.

For example, if with this command we want to do something with the data, with this we will know that the search failed and the reason for it. At the same time if we do multiple searches, we change the parameter from “Stop” to “SilentlyContinue” so that the script continues if one of these fails.

Other possible errors in this function

Saying that the “ErrorAction” parameter addresses errors in a simple way, but if we want a little more control over them, we can use “Try / Catch”. This will allow us to separate the script when the “cmdlets” return an error. This command is divided into two parts, the first is where we are going to execute the code normally. Here we can still use the -ErrorAction parameter, but it can only be set to Stop. An example is the command “Try {Get-Process« Cortana »-ErrorAction Stop}”.

Now that we have created a test block of “cmdlets”, if we want to define what to do when it fails, we use the second part, Catch. For example, if necessary, we can receive a Process not found message, writing “Catch {Write-Host» Process not found ”}”.

To end this section, if we want something to run regardless of whether the “cmdlet” fails or not, we can use Finally, which is added after Try and Catch.

Use error variable in Windows Powershell

Now that we know how to handle errors and how the script responds to them, we can go a step further to find out what really went wrong with the “-ErrorVariable” parameter. And it is that the errors are written in a custom variable. Therefore we first define our own variable, for example “$ SearchError”. Of course, when we call this variable in the “ErrorVariable” command, we no longer use the dollar sign, and we add it to the end of the line.

“Get-ChildItem -Path« ~ \ Documents \ *. Jpg »-Recurse -ErrorAction Continue -ErrorVariable SearchError”.

It is worth mentioning that if we add a + sign in front of the name of the variable, we can add another instead of replacing it directly. For example we can instead use this output and the Try / Catch commands to write custom output, among many other things.

This is what will allow us to obtain a record of any errors and keep track so that things run successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button