Powershell basics part 2 – Customizing powershell, operators

In this post I will talk about customizing powershell enviroment and some other cool features.
First of all I will start by creating a windows powershell desktop icon:

Run as administrator
After the icon is created right click it and then click properties. In the properties window in the start in field enter the path where you want to start powershell in. I’ve created previously a folder named Powershell in drive C, so I’ve enter the path C:\Powershell. Press Apply and the OK. Now if I start powershell it will start in the path added:
Windows Powershell
Now I’ll go on top of the window, right click it and then I’ll press properties:
In this windows you can customize powershell features like Cursor Size (Small, Medium, Large), Command History(Size, Buffers) etc. Now let’s press the font tab:
Customize Powershell
Here you can change the font options. I usually use 8×12 and Raster Fonts. Now press Layout:
Customize Powershell
In the Layout tab you can customize window size and position. Let’s go to the final tab Colors:
Customize Powershell
Here we can change the colors of different items like screen text, screen backgroud, popup text etc. After you finish press ok and the start powershell.
A nice cmmdlet is Get-History; type this and then press enter. Powershell will save the commands that you have previously used in a buffer so the commands that you’ve typed will appear here. The number that powershell memorizes is the “Buffer Size” option that you have entered in the window properties. Here is the output of the command:
Get-History command
Command Get-PSProvider is another useful cmmdlet that displays information about the specified Windows Powershell provider. Here is the output of the Get-Help Get-PSProvider:
Now type Get-PSDrive. This command gets the windows powershell drive in the current session:
Get-PSDrive Powershell
OK now we will use the command Set-Location (you cand use also cd-change directory alias) and we will switch to the second partition. I will enter Set-Location D:
Set-Location command
Now enter Set-Location Env: and after that enter Get-ChildItem:
Set-Location Env Powershell
This is a environment that displays the variables need by the OS (operating system).
As you have probably seen in the output of Get-PSDrive command, there where also the registry containers in the output (hklm and hkcu – for the local machine and current uset). Just as if you typed regedit and entered the Registry Editor window, in powershell you can also change the registry settings. I entered Set-Location hkcu: command and then Get-ChildItem. In parallel I opened a regedit window. Here is the output:
Get-ChildItem Powershell
As you can see the output is relatively the same. OK, now if we want to start a clear powershell session we can enter the command Clear-Host ( you cand use also the alias cls).
When scripting you’ll have to know some syntax used. A script written in powershell has a .ps1 extension.
# is used to write a description or something that powershell will not read.
Write-Host, will send a message to the screen.
Read-Host, reads a line of input from the console.
Variables are written as $ followed by the variable name. You can find out more about variables by typing Get-Help about_variables.
Now let’s start a simple script. Let’s make a summation between two variables, a and b. The result we will enter in variable d. OK, let’s type the following:
$a = 15
$b = 20
$d = $a + $b
write-host $d
Powershell operation
I will write now different operators that you need to know in order to work with powershell. I’ve written them from the Get-Help about_operators command:
 Arithmetic Operators
     Use arithmetic operators (+, -, *, /, %) to calculate values in a command or expression.


 Assignment Operators
     Use assignment operators (=, +=, -=, *=, /=, %=) to assign one or more values to variables, to change the values in a variable, and to append values to variables.
 Comparison Operators
     Use comparison operators (-eq, -ne, -gt, -lt, -le, -ge) to compare values and test conditions.
Logical Operators
    Use logical operators (-and, -or, -xor, -not, !) to connect conditional statements into a single complex conditional.


Redirection Operators
    Use redirection operators (>, >>, 2>, 2>, and 2>&1) to send the output of a command or expression to a text file.
Split and Join Operators
    The -split and -join operators divide and combine substrings
 Type Operators
     Use the type operators (-is, -isnot, -as) to find or change the .NET Framework type of an object.


Unary Operators
    Use unary (++ / ) operators to increment or decrement variables or object properties and to set integers to positive or negative numbers.
Special Operators
    Use special operators to perform tasks that cannot be performed by the other types of operators. “
I will talk about special operators in a future post.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s