PowerShell – What you need to know to get started

Introduction

The objective of this post is to give you the key elements to permit you finding your path dealing with Powershell espcially if you are a new comer.

Target audience ( Yes I need to specify πŸ™‚ ):

This article is not for people with advanced powershell skills (sorry guys πŸ˜‰ ) as I try to help beginers to jump on the “Powershell train”.
But don’t hesitate to read and comment, always room for improvments

Powershell is a fantastic tool but as all programming language it has its own rules based on syntax and interoperabilities with different components.

In the IT community some people will tell you that PS is not a programming language others will tell you the opposite. My little background in studying VB pushes me in the second group.

I let you decide if yes or not it’s a programming language! Enjoy the lecture and the learning

Rules of thumb: Objects, Objects and Objects!! :-)

PS is an object oriented language. It means that you will manipulate or get informations about objects. An object can be a NIC card, a VM, a storage array setting, even a .csv file.

With powershell you can split your interactions with objects in 4 main categories:

  1. Modify
  2. Create
  3. Delete
  4. Read

wait a second! πŸ™‚ I told you earlier (Powershell Introduction) that you can do a multitude of things with Powershell and now I say only 4 things.

Let’s take a step back! PS is an object oriented language, what do you need more when dealing with objects? If you still have some doubts, let’s take the example of a file: What do you want to do with a file? Euuhh,… Modify, create, delete and read. Bingo! Here it is!

Now, the beauty of powershell is that you have a multitude of ways to accomplish the same thing and you can play on a multitude of objects at the same time.

Still not convinced, here is an example about the options you have for getting the same information about a specific disk on a machine:

In this example, I wanted to get the name of my disk number 6 on my machine. You can see that I’ve wrote 2 lines to get the same result

  1. Long way
  2. Short way

Result: The same in both cases: “Kingston DataTraveler 3.0”

Before discussing more about commands and because we’re talking of objects in Powershell, here is a list of objects that are interesting when you learn Powershell. It’s not exaustive at all, but I need to restrict myself for this post.
If you want to see more about Powershell objects, I strongly advise you to read about_topics in help (help will be detailed later in this post)

ObjectsDescription
AliasesAn alias is a nickname for a specific cmdlet. You can create your owns
VariablesA variable is an object that you store in memory to be re-used later in a script or in an advanced command by PS
FunctionsA function is a command or a script for which you assign a name. The function can have parameters as commands
PSProvidersPSProviders are objects that render visible data that is residing outside Powershell (instance / session)
ModulesA module is an extension of Powershell which contains its own cmdlets, functions, help files. Most of the time, it is developped by 3rd parties, like Amazon, HP, VMware… This way it extend the PS footprint.

Powershell Commands

Of course! Commands! πŸ™‚ As you need to interact with objects, you need commands to accomplish the magic!

Powershell commands can be categorized as follow:

  1. cmdlets -> is the native command-line utility that is only present in Powershell ecosystem. It is written in the .NET Framework
  2. functions -> You can see a function as a group of cmdlets. Typically a script that is written in PS scripting language.
  3. Applications -> An application is a command understood by powershell but which is coming from outside Powershell framework (for instance a command coming from your regular Windows command-prompt such as ping, telnet, diskpart…

Understanding the cmdlet mechanism and syntax

The best way to explain the syntax is an example. So here under you will find a powershell command that illustrates all its components.

Powershell-Command explained

The cmdlet itself (1 & 2 on the picture) is always composed of a verb and noun seperated by a dash sign (-). No space in between
Typically action on object.

A parameter (3, 5 on the picture) is always preceded by a dash sign (-). When needed, you can or must add a value (4 & 6 on the picture) to this parameter.
You always put a space between a parameter and its associated value
Typically it is how you do the action on a object

A switch parameter (7 on the picture) can be added at the end of the command but is not linked directly to it and is not mandatory as well (it really depends of the cmdlet you use).

With powershell nothing is case-sensitive!!! Thanks to the creators of Powershell.

If you’re interested to get more info about Powershell syntax you can run the following command: Get-Help about_Command_Syntax

Cool stuff about commands in Powershell

Autocomplete

A very cool feature that unix guys know for a while but was not present for Windows guys is “autocomplete”.
Whatever your experience with powershell and because you cannot recall every single command syntaxt, you can use the auto-complete feature to help you. Start typing Get- in you powershell console and type on TAB key of your keyboard to receive suggestions from powershell. This feature will help you a lot.

Commands history

Another very cool feature with Powershell is the command history. Again, unix guys know this for while which was not the case for Windows guys.
With Windows Command prompt when you close and re-open it again, you are not able to retreive your commands you typed before, which could be frustrating especially when you spent hours to make a command working (complex of course πŸ™‚ πŸ™‚ πŸ™‚
How the magic happens with history? Simply because of the presence of a file located in your machine as shown here under:

For those who want to get the path ready for their own checks, here is the path:
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline

!! Attention!!! it’s only valid if you have at least Powershell V5 on your machine.
You can quickly verify by running the following command in Powershell console:
Get-host

You should obtain a result like this:

Be aware that this file stores 4096 commands by default. which means that each time the limit is reached, it overwrites the file. You can play with the size of the file by changing a Powershell variable

$MaximumHistoryCount

I’m not going to go in the details for that as it’s not the objective of this post, maybe it could be a topic for a more detailed post about that

Powershell Help

For this one, I can really dedicate a full post, maybe I will do it. For the good handling of this post I will give you the bird’s eye view on this.

All the IT pros using Powershell will tell you that the best thing you can do to help yourself in your PS journey is the constent usage and good understanding of PS help.
Whatever you write a script or executing a simple command, PS help is always there to help you πŸ™‚
Even with 6 years of experience dealing with PS, we still use PS help.

Microsoft identifies 2 categories of help files. It identify them as follow:
1. Help files for cmdlets ( syntaxt example: Get-Help Get-eventlog)
2. Help files for concepts (Get-Help about_profiles)

Get-Help comes with a set of built-in parameters that will help you differently regarding which piece of the puzzle you miss.
Here is a list of parameters (not exaustive) that you can find with Get-Help cmdlet:

  • Get-Help Get-Eventlog -detailed
  • Get-Help Get-Eventlog -full
  • Get-Help Get-Eventlog -examples
  • Get-Help Get-Eventlog -online

I let you try on your own to identify the differences between all of them! But I promise, you will notice differences πŸ˜‰

Updating help

As the rest of your computer, you need to update the help content of Powershell. Why? simply because it will help you to receive the most accurate information regarding a command or concept. Sometimes, errors are reported, a new command is available and you don’t have help information for that.
After using it for some time you will see that this cmdlet will be one of your best friend.

Note! to update your help files, you need to run the following cmdlet Update-Help in a elevated session (Administrator session)

If you do it in a regular session, it will fail as you alter the system.
Depending on the environment you work, you may not be able to run a PS session in Adminisrator mode, probably due to security policies put in place in your company. Refer to them to help you in updating your Help files.

Powershell Session

A powershell session is the instance of Powershell you run locally, remotely or via API.
To complement this short description, here is a picture that is illustrating easily the concept of session

Not all Powershell objects live across different sessions. Variables are the perfect example of this.
For instance, I can create a viariable in a local session (Powershell console interacting with local computer), create a variable and close this session.
Next, I reopen a session, type the same variable. Result Powershell doesn’t know anymore.

As demonstration is always better, here are the screenshots πŸ™‚

Session 1:

Powershell variable across session 1

Session 2:

Powershell variable across session 2

Why? What’s happening under the hood?

The answer is quite evident when you have a closer look at the annotations in the screenshots.

Powershell is an executable, and because of that, each Powershell session run in its own memory pages. This means that the information is not shared between sessions.

Powershell Session – Memory usage

When the session is closed all related objects present in the memory pages are cleared from memory (RAM).

Statement: All objects (variables, functions, aliases…) created by a user have a lifetime equivalent to the one of the PS session.

This natural behavior is quite ennoying when you want to reuse the same objects over sessions. That’s why the creators of Powershell wanted to help and render your work more comfortable πŸ™‚ πŸ™‚

They created: Powershell profiles which is detailed in the following section of the post

As a last note: Powershell system objects are persitent across sessions.

Powershell Profile

Last (component) but not least for this post! πŸ™‚

Powershell profile is powershell script that is located in a specific place on your drive. You can configure everything you need for your different PS sessions (variables, functions, modules…)

Each time you launch Powershell, it will load all the objects you configured in the Powershell profile file. So depending of what you configured in that script, the Powershell session will take more or less time to start.

Prerequisite:

To be able to run a powershell profile you need to configure Powershell to run script via the following cmdlet.

Set-ExecutionPolicy

Please check the help if you are not sure how to use it via: Get-Help Set-ExcutionPolicy as it could expose your computer to security risks.

If you have a computer managed by an IT team within your company, maybe the best option is to check with them what is the best approach for your needs.

For my homelab environment, here is the full command I type. Security in this case is not a concern:
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Unrestricted
As this one removes any security checks, I would recommend you to execute that one in a isolated lab environment only.
Depending of your environment, you will need to modify the syntaxt.

In Powershell, you need to know that you have 6 Powershell profile files.
Here is the table which details locations and purposes

DescriptionPath
Current User, Current Host (local machine/console)$Home\[My] Documents\WindowsPowershell\Profile.ps1
Current USer, All hosts$Home\[My] Documents\Profile.ps1
All users, Current Host (local machine/console)$PsHome\Microsoft.PowerShell_profile.ps1
All users, All hosts$PsHome\Profile.ps1
Current USer, Current Host (ISE)$Home\[My] Documents\WindowsPowershell\Microsoft.PowershellISE_Profile.ps1
All users, Current Host (ISE)$PsHome\Microsoft.PowerShellISE_profile.ps1
PowerShell profiles and their paths

Additional resources to help you in your Powershell Journey (they still help me today πŸ™‚ )

Whatever your level with Powershell, you will always need help that will permit you to acheive your goals.

Here is the list of resources that are helping me:

Book TitleDocument typeLink
Learn Windows PowerShell in a Month of LunchesBook or E-bookhttps://www.amazon.com/Learn-Windows-PowerShell-Month-Lunches/dp/1617294160
Step by Step Windows PowershellBook or E-bookhttps://www.amazon.com/Windows-PowerShell-Step-Ed-Wilson/dp/0735675112
Powershell cheat sheetPDF posterhttps://www.powershellmagazine.com/2014/04/24/windows-powershell-4-0-and-other-quick-reference-guides/
External references

Conclusion

I hope this post helped you to get a better understanding of PowerShell basics. I hope, at least, it gaves you the envy to learn or practice it.
Today as IT admin, working on servers or computers without knowing how to automate tasks, is a real break in your career so don’t hesitate to jump in and practice powershell.

I hope this post helped you,
Dont’ hesitate to comment, like or rate,
Your host,

You can always reach me here: