Please share this tutorial, that inspires me to continue doing this tutorial series.
Beginners guide to Powershell — Part 1
This idea to teach Powershell was on top of my list, I started to write a book about Powershell and the title would be used as the primary name for the book. Anyhow, I’m just going to write a blog to help users get started in Powershell.
If you are an IT admin but don’t know how to write code or even start, well you are probably in the same situation as many IT admins are in. That being said, how do you start learning Powershell. Well, the first thing you should know what Powershell is and what it is used for. Powershell is mostly used for developing scripts to automate administrative tasks such as creating users, installing software, mapping drives or even install a whole environment with all the Windows features required to speed up development. Admins use Powershell differently but in commonly used to simplify tasks.
But what is Powershell? Powershell is an interpreted language where there is an interpreter between your script and the operating system. Some development systems such as C++ or C# have compiled language where the code is compiled into a binary where the code can talk directly to the operating system or whatever system you are creating for. For instance, if you are developing in the Go language the code can be compiled as a binary for different operating systems without changing the code.
The development process of Powershell begins using an IDE or Integrated Development Environment, where that environment helps you create scripts and or even help you debug the script if you have any errors in it. There are two common IDE’s that are used and are Powershell ISE and Visual Studio Code and I use the latter in most cases. Powershell ISE is used for creating quick simple tasks that do not require any specific debugging but Visual Studio Code includes a more in-depth debugging solution. If you are developing for a company then you would start by standardizing the code using snippets to help you have the structure of the code always the same.
The snippet is a predefined code or a structure of code.
If you want to use Visual Studio Code, then you can download them from this website https://code.visualstudio.com/ and when you are done you should install a plugin for Powershell. If you want to use Powershell ISE then it is integrated into the Windows operating system at least Windows 8 or later.
Getting the feel to code
The first thing you need to know is how you can write output into the console with some text. Use the following code block below to see the results.
write-host “This is something that will display in the console.”
You can also read input from the user by using read-host, this will prompt the user to type in some information and will be read when the user presses enter.
read-host “Type in your age ?”
But how can I save the information that I typed and keep it in memory? Well, Powershell has something called variables, these variables can contain different types of information that you are working with. As in this example code block below, we are getting some information into a variable, where the variable is then saved to memory and we can use it later.
$myage = 42
It’s worth mentioning that there are different types of variables that exist, I’m not going into them all but just a few. They are called data types and define what the variable type is e.g. in the code block above the variable contains the number 42 which means that the type of the variable is an integer, but if we would set the variable as seen in the code block below the datatype of the variable is called a string.
$myname = “Gisli”
We can join some of these variables and commands as seen in the code blocks above and join them we can create our first simple program.
$myname = read-host “What is your name?”$myage = read-host “What is your age?”write-host “Your name is $myname and your age is $myage years old”
The code block above is reading the input from the user into a variable both the name and the age and then it will write the output into the PowerShell console. If I would type the name Gisli into the myname variable and the number 42 in the age the output would be like this “Your name is Gisli and your age is 42 years old”.
It’s worth mentioning that in older coding languages such as C or C++ you had to point to a memory address to save the information and then clean up after by removing the pointer to the memory. In Powershell, there is something called garbage collector where you don’t have to worry about memory addressing or even removing from memory where the Powershell interpreter does it for you.
Continue with the next part of this series