Some special operators in PowerShell: $($Variable), @( ), &, S_., %, ?

There are some operators that they are not common in PowerShell and we have problems to figure out the logic when we are reading a script written by another person.

For example $($Variable) operator. This is a subexpression operator that always will evaluate first the expression contained into the parenthesis and return the value as an array, and therefore you can use this information directly. Let’s see the follow example:

$c = Aduser 430001150
The variable $C contains all the information for this Active Directory object, and therefore If I want to know only the first name and last name I can write this:

Write-Host “The firstname is: $($c.GivenName)-ForegroundColor Yellow
Write-Host “The firstname is: $($c.Surname)-ForegroundColor Yellow
And the output will be:
The first name is: Tony
The last name is: Gonzalez

@() Array expression

Returns the result of one or more statements as an array, when you have the construct @() you are creating an array without any elements at all. Otherwise, if you add elements to the array they will have an index;

$array = @()   #We declare this variable as array
$array = @(1,2,5,6,9,”Hello”,”World”)   #We are adding elements to the array, the first element has the index 0, in this case has the value 1.
$array[0]
1
If we want to know what information contains this array:

PS C:\> $array
1
2
5
6
9
Hello
World
& Call operator. We use this operator to run a command, script, or script block. For example:

PS C:\> $b = “Get-ChildItem”
If we execute this value to see the result, will be the string stored in quotes.

PS C:\> $b
Get-ChildItem
But if we put the call operator & we are going to get another result

PS C:\> & $b
    Directory: C:\
Mode                LastWriteTime     Length Name                                                                                                                                                
—-                ————-     —— —-                                                                                                                                                 
d—-         12/7/2016   2:52 PM            Dell                                                                                                                                                  
% Is an Alias for the command let ForEach-Object is a loop who returns each pipeline object one after the other. Which means that you can use both in a sentence.

? Is an alias for the command let Where-Object. We use this sentence to select an object from a collection.

In both aliases we can use $_. To reference to a filed into the result. For example:

PS C:\> Get-ChildItem “C:\” | ?{$_.Name -eq “temp”}
    Directory: C:\
Mode                LastWriteTime     Length Name                                  
—-                ————-     —— —-                                  
d—-          2/1/2018  11:51 AM            temp                                   

These are only some of the operator that sometimes we do not know how they mean when we are analyzing a script that has been created by another person, but obviously, there are much more. 

I will put another post with a little more. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s