Listado de la etiqueta: PowerShell


PowerShell offers a variety of comparison operators that are used to achieve a couple of functionalities such as matching some particular patterns and comparing different values. There are different categories of the comparison operators such as equality, replacement, matching, etc. If we talk about the like operator it falls in the category of match operator.

In this post, we will explore the different aspects of the “like” operator in PowerShell “Where-object” as listed below:

  • What is a “like” Operator in PowerShell?
  • Basic Syntax
  • How to use a “like” Operator in PowerShell Where-Object?

So let’s begin!

What is a “like” Operator in PowerShell?

-like, -match, -notlike, -notmatch, all these operators belong to the same category i.e. “Matching”. These operators are used to find out whether a specific string matches the wildcard/regex pattern or not. If we talk about the “like” Operator, it is specifically used to match a wildcard pattern within a string.

Basic Syntax

The below snippet shows how to use the like operator in PowerShell:

The like operator will return true if a match is found in the string, and false if the matching value is not found in the targeted string.

How to use a “like” Operator in PowerShell Where-Object?

This section will explore how to use the -like operator in PowerShell, to do that, it will consider a couple of examples.

‘Welcome to linuxhint.com’ -like ‘*welcome*’

In the above-given piece of code, we were looking for the word “welcome” in the string, and to do that we utilized the -like operator along with the wildcard “*”. The output for the above cmdlet will go like this:

The -like operator returned true, it verified that the match “welcome” found in the specified string.

Let’s modify the cmdlet a little bit to check what will happen if didn’t utilize the wildcard operator:

‘Welcome to linuxhint.com’ -like ‘welcome’

When we run the above cmdlet, we get the following result:

What went wrong this time? Why do we get a false value?

In the first example, we utilized the wildcards which say that there can be some text on either side of the targeted word i.e. “welcome”. While the string in which we didn’t utilize the wildcards will look for the exact match (nothing more, nothing less). This means it will return true only if it finds the exact word i.e. there is not a single character on either side of the targeted word as shown in the below snippet.

Let’s run the below cmdlet on Windows PowerShell:

‘welcome’ -like ‘welcome’

The output shows that this time the like operator returns true.

Conclusion

In PowerShell, the “like” Operator belongs to the category of matching operators and is specifically used to match a wildcard pattern within a string. The like operator will return true if a match is found in the string, and false if the matching value is not found in the specified string. This post explained how to use the like operator in PowerShell Where-Object with the help of some suitable examples.



Source link


In programming as well as scripting languages including PowerShell, experiencing decision-making scenarios or dealing with different conditions are very common. PowerShell deals with such situations using conditional statements/expressions such as if, else-if, etc. These decision-making statements are responsible to manage the program’s flow based on different conditions. Most of the time, “If” and “else” statements are used collectively so that the script must run in every case.

This write-up will present a thorough guide for the if-else statements:

  • What is if-statement in PowerShell?
  • Syntax of if-statement.
  • What is if-else statement in PowerShell?
  • Syntax of if-else statement.

So let’s get started!

What is if-statement in PowerShell?

The if statement in PowerShell takes an expression/condition in its parenthesis and tests it. Consequently, it will return either a true or false value, if the specified condition is true then the code-block associated with the if-statement will get executed. The if-statement deals with the true condition only, it has nothing to do with the false condition.

Syntax of if-statement

The below-given snippet shows the basic syntax of if-statement in PowerShell:

if(expression/condition) {
   // Executes only if the given expression is true
}

Let’s consider the below script to understand the working of if-statement in PowerShell:

$a =12;
$b =15;
if($a -le $b) {
  write-host(«a is less than or equal to b»);
}

In this example program, we utilized the if-statement to test an expression, if the returned value is true then the body of if-statement will execute else not:

The output verified the working of the if-statement.

What if the returned value of the specified expression is false? How if-statement will deal with the false value?

$a =12;
$b =15;
if($a -ge $b) {
  write-host(«a is less than or equal to b»);
}

The above script will generate the following output:

The cursor moved to the next line without performing any specific task. It verified that the if-statement doesn’t handle the false conditions.

What is if-else statement in PowerShell?

To tackle the false conditions, the else statement can be used along with the if-statement. In Powershell, if we utilized the combination of if and else statements, as a result, both true and false conditions will be tackled.

Syntax of if-else statement
The below snippet depicts the basic syntax of the if-else statement in PowerShell:

if(test-condition/expression) {
   // Executes only if the given expression is true
}
else{
    // Executes if the specified expression is not true
}

How to use if-else statement in PowerShell
Below snippet will assist you in this regard:

$a =20;
$b =15;
if($a -le $b) {
  write-host(«a is less than or equal to b»);
}
else{
  write-host(«a is greater than b»);
}

This time we utilized both if and else statements, now if the value of a is less than or equal to the b then body of if-statement will execute otherwise the body of else-statement will execute:

The above snippet verified that the else-statement was executed because the specified condition was false.

Conclusion

In PowerShell, decision-making statements such as if, else, and else-if are used to manage the program’s flow based on different conditions. The if-statement deals with the true condition only while the else-statement deals with the false condition only. Therefore, in PowerShell, if and else statements can be used combinedly to handle both true and false conditions. This write-up explained all the basics of if and else conditions in PowerShell using some suitable examples.



Source link


PowerShell is a convenient application used to automate the system’s management, run commands/scripts, tasks automation, etc. It is a cross-platform tool that can run on different operating systems such as Windows, Linux, and Mac. It is composed of a Command shell, configuration management frameworks, and scripting language. It is very much similar to the Windows Command prompt; however, it comes up with some advanced tools/features for example it is available across multiple platforms, it provides task automation solutions, etc.

Considering the key features of PowerShell you must be thinking about getting started with PowerShell. But worried about how to write code/script in PowerShell! If that’s the case then this write-up is going to assist you in this regard from the scratch.

Before moving toward the main topic let’s have a look at the content that we are going to cover in this write-up:

So, let’s begin!

What is a Script?

In PowerShell, a script is a set of instructions stored in a text file with the extension “.ps1”. PowerShell will understand and execute these instructions in a sequence to achieve different functionalities.

How to create a script file?

To write code in PowerShell, we have to create a script file first and then we can write the code within that file. We can create a PowerShell script file using any text editor, or PowerShell ISE. In this write-up, we will create a script in PowerShell ISE.

The below-listed steps will assist you to create a Script File in PowerShell ISE:

Press “Win + R”.
A new window will appear, type “powershell_ise.exe”:

Press “Ok”. Consequently, the following window will appear:

Here, in the scripting pane, we can write the code while the output will be displayed on the console. Moreover, we can run the code/script file from the console. The PowerShell script file will always be saved with the .ps1 extension.

PowerShell set execution policy

If you are running your first program/script in PowerShell then you have to set the execution policy as RemoteSigned. To do that you have to run the PowerShell as administrator as shown in the following snippet:

Next, copy the below-given command and paste it into the Windows PowerShell:

Set-ExecutionPolicy RemoteSigned

Allow the permissions and hit the “Enter” key to set the execution policy to remote signed.

How to run a program/Script in PowerShell?

We can run a script file or code using two different ways i.e. using “console”, and using the “run” button.

How to run a script file or code from the console?

To run a script, we have to type/specify the “.” followed by the complete path of the file on the console:

Afterwards, press the “Enter” to run the script/code.

How to run a script file or code using the run button?

The second way to run a code is very simple i.e. press the run button as mentioned in the below snippet:

How to write your first program in PowerShell?

The below example will let you understand how to create a simple program in PowerShell ISE:

In this example program we will utilize the “Write-Host” command to print a simple string on the console:

Write-Host «Welcome to linuxhint.com»

This is how we can get started with the PowerShell.

Conclusion

To write code in PowerShell, we have to create a script file first and then we can write the code within that file. A PowerShell script file can be created using any text editor like notepad, VS code, or PowerShell ISE. The script file must be saved with a “.ps1” extension, and it can run from the “console” or using the “run” button. This write-up explained how to create a script file, how to write code in a script file, and how to run a script file using the PowerShell ISE.



Source link