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:
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:
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:
Here you can change the font options. I usually use 8×12 and Raster Fonts. Now press Layout:
In the Layout tab you can customize window size and position. Let’s go to the final tab Colors:
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:
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:
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:
Now enter Set-Location Env: and after that enter Get-ChildItem:
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:
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
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:
Use arithmetic operators (+, -, *, /, %) to calculate values in a command or expression.
Use assignment operators (=, +=, -=, *=, /=, %=) to assign one or more values to variables, to change the values in a variable, and to append values to variables.
Use comparison operators (-eq, -ne, -gt, -lt, -le, -ge) to compare values and test conditions.
Use logical operators (-and, -or, -xor, -not, !) to connect conditional statements into a single complex conditional.
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
Use the type operators (-is, -isnot, -as) to find or change the .NET Framework type of an object.
Use unary (++ / —) operators to increment or decrement variables or object properties and to set integers to positive or negative numbers.
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.