Quantcast
Channel: PowerShell Basics Archives - TechGenix
Viewing all articles
Browse latest Browse all 84

What does a question mark mean in PowerShell commands?

$
0
0

A few weeks ago, I received an email message from someone who is relatively new to PowerShell. They had followed some of the techniques that I outlined in some of my other articles and mimicked my results but were still not entirely clear on some of the syntax elements. In their email message, the person asked a very simple question with a not so simple answer. The question was, “What does it mean when you see a question mark in a PowerShell command?”

The reason why the answer to this question isn’t quite so straightforward is because a question mark can mean several different things, depending on the context of how it is being used. While I’m not going to pretend that this article covers every conceivable way that a question mark can be used in a PowerShell script, I want to show you three of the more common use cases.

PowerShell question mark as an alias

Sometimes when you see a question mark used in a PowerShell script, it is acting as an alias to the Where-Object cmdlet. If you are not familiar with the Where-Object cmdlet, it is generally used for filtering pipeline output.

Suppose for example, that I wanted to find out which system services on my Windows PC are currently stopped. I could use the Get-Service cmdlet to retrieve a list of services, but the results would include all services whether they are running or stopped. However, I can pipe the Get-Service cmdlet’s output into the Where-Object cmdlet and then set up a filter so that only objects (system services) with a status of Stopped are displayed. Here is what such a command would look like:

Get-Service | Where-Object {$_.Status -eq ‘Stopped’}

You can see what this looks like in the screenshot below.

powershell question mark
As previously mentioned, you can use a question mark as an alias for the Where-Object cmdlet. This means that you can literally replace the words Where-Object with a question mark and the command will work the same way. Here is what the previous command would look like, using the Alias:

Get-Service | ? {$_.Statis -eq ‘Stopped’}cls

As you can see in the next figure, this command produces exactly the same output as when I used the Where-Object cmdlet.
powershell question mark

Evaluating a command’s success

As I’m sure you have already figured out, question marks in PowerShell scripts do not always take the place of the Where-Object command. Question marks can also be used as a tool for evaluating a command’s success.

When a question mark is being used for this purpose, it will be proceeded by a dollar sign, thereby making it a variable named $?. This variable will always return a Boolean value. It will contain a status of True if the previous command succeeded, and a value of False if the previous command failed.

Keep in mind that the value of the $? variable has nothing to do with whether or not the command in question gave you the results that you wanted. It only reflects whether or not the command produced an error. If you look at the screenshot below, for example, you can see that I intentionally entered an invalid cmdlet (Get-VMS), which caused PowerShell to produce an error. When I output the contents of the $? Variable, the variable is set to False. Next, I enter the Get-VM cmdlet (which is a valid cmdlet). This time, when I output the value of the $? variable, I get a result of True. This indicates that the most recently entered command did not produce an error.

The $? variable returns True or False depending on whether or not the most recent command caused an error.

So, what might something like this be used for in real life? Well, evaluating a command’s success can be handy for error handling. I have seen scripts in which a particular command is known to produce an error under specific circumstances. The error message itself can be suppressed by telling PowerShell to silently continue, but because an error has occurred some type of corrective action may need to be taken (depending on what the script is doing). A line within the script could use the $? variable to check and see if the command in question has failed. If a failure has occurred then the script could call a function whose purpose it is to handle the error.

Ternary operators

Starting with PowerShell 7, question marks could be used in the creation of a ternary operator. A ternary operator is essentially just a more concise way of writing an if then else statement.

When you create a ternary operator, you will need to specify a condition followed by a question mark. From there, you must specify the action to take if the condition is true, followed by the action to take if the condition is false. To demonstrate how this works, let’s tie it into the $? variable that I showed you in the previous section.

As you will recall, $? reflects a value of True if the previous command succeeded, and a value of False if the previous command returned an error. As such, we could create a ternary statement that looks like this:

$? ? ‘The Command Succeeded’ : ‘The Command Failed’

As you can see in the next screenshot, I used the Get-VM cmdlet to retrieve a list of virtual machines and then used a ternary operator to evaluate the command’s success. I was greeted with a message saying that the command succeeded. Likewise, I entered an invalid command and then tried the ternary operator again. This time I received a message stating that the command failed.


You will notice that the PowerShell session in the figure above has a black background, while the other screen captures have a blue background. This is because ternary operators only work in PowerShell 7, which is not the version of PowerShell that is installed in Windows by default.

Featured image: Pixabay

The post What does a question mark mean in PowerShell commands? appeared first on TechGenix.


Viewing all articles
Browse latest Browse all 84

Trending Articles