6 min read

PowerShell is Microsoft’s automation platform for IT Pros. Of late, there have been a lot of questions around the complexity of this latest automation tool by Microsoft. At Microsoft Ignite 2018, Jason Himmelstein, Director of Technical Strategy and Strategic Partnerships, Office Apps & Services MVP, explained the basics of PowerShell and how to truly optimize your SharePoint implementation using this powerful IT pro toolset. While in this post we look at the big picture, you can check out the complete video here: ‘Introduction to PowerShell for the anxious IT pro’.

Want to do more with PowerShell?

After learning the basics, you can learn how to use PowerShell to automate complex Windows server tasks. You can also improve PowerShell’s usability, and control and manage Windows-based environments by working through exciting recipes given in Windows Server 2019 Automation with PowerShell Cookbook – Third Edition written by Thomas Lee. 

Himmelstein starts off by saying PowerShell isn’t a packaged executable, nor it is developer-centric that needs one to understand code, and it is easy for an IT Pro to understand.

What is PowerShell?

Windows PowerShell is Microsoft’s task automation framework, consisting of a command-line shell and associated scripting language built on .NET Framework. It provides full access to COM and WMI, enabling administrators to perform administrative tasks on both local and remote Windows systems.


In simple words, PowerShell is an object-based, not a text-based, command-line interface for Microsoft Technologies. This means results in PowerShell can be acted upon and not just read from. One can cause huge damage to an environment using PowerShell as there is no back button in PowerShell. However, to check what must have gone wrong, you can check the logs but can not undo actions.

Why PowerShell matters

Regardless of the platform a person uses such as Office 365, Azure, etc., PowerShell can be easily implemented due to its cross-platform capability. Himmelstein also highlights one can also get started with Azure PowerShell by trying it out in an Azure Cloud Shell environment, an interactive, authenticated, browser-accessible shell for managing Azure resources.  Azure Cloud Shell comes equipped with commonly used CLI tools including Linux shell interpreters, PowerShell modules, Azure tools, text editors, source control, build tools, container tools, database tools and more. Cloud Shell also includes language support for several popular programming languages such as Node.js, .NET and Python.

Cloud Shell also securely authenticates automatically for instant access to your resources through the Azure CLI or Azure PowerShell cmdlets. Users can use PowerShell in Cloud Shell. One can also develop applications using PowerShell or can use PowerShell via Source Control Management (SCM).

Basics of PowerShell

PowerShell Hardware

There are two ways one can use PowerShell; one is via the PowerShell Console, which is similar to a command line. The other is PowerShell ISE (Integrated Scripting Environment). One thing Himmelstein encourages is, “we run PowerShell in the Console and we write PowerShell in the ISE.” The reason is there are certain functionalities that do not work in the ISE when one hits the ‘Run’ command. In such cases, the user will have to take that PowerShell out, copy it, save the file and run it in a command window.

cmdlets

Cmdlets are the main building blocks of PowerShell. These are mini commands that perform one action. These have the ability to pipe the output of one cmdlet into further cmdlets. These can also perform equality tests with expressions such as -eq, -lt, -match; one can diff easily within a PowerShell.

Modules

There are four types of Modules in PowerShell:

  • Script: A Script module is a file (.psm1) that contains any valid Windows PowerShell code.
  • Binary: A binary module is a .NET framework assembly (.dll) that contains compiled code.
  • Manifest: A module Manifest is a Windows PowerShell data file (.psd1) that describes the contents of a module and determines how a module is processed.
  • Dynamic: A dynamic module does not persist to disk. It is created using New Module, is intended to be short-lived, and cannot be accessed by Get-Module.

Himmelstein prefers not to use the Dynamic module as it persists for just one session.

Objects and Members

Objects are instances of classes and have properties and methods.

Members are properties and methods of an object. Properties define what an Object is and Methods define what you can do with the object.

Himmelstein puts together all these terms in a simple way:

  • Objects = stuff
  • Cmdlets = things you can do with the stuff
  • Modules = list of things you can do with the stuff
  • Properties = details about the stuff
  • Methods = instructions for things you can do with the stuff

PipeLine

Using PipeLines one can chain objects together for processing. The output of a pipelined object becomes the object itself.

Functional Explanation

  • Get-command: Gets all the cmdlet installed on your computer.
  • Get-help: Displays additional information about a cmdlet
  • Get-member: Listing the Properties and Methods of a Command or Object
  • Get-verb: Gets approved Windows PowerShell verbs
  • Start-transcript: Logs everything you do in that PowerShell window to a file
  • Get- history: If you didn’t start transcript, you can still review your history before closing your Shell or ISE window.

Tips for PowerShell beginners

  • Use Variables: You can use any variables except the ones that are reserved by the system, which you will be prompted when you try to enter a reserved variable.
  • Call one thing at a time
  • Comment your scripts as this may save you a lot of time.
  • Create scripts using an ISE/IDE, you can also use the Visual Studio Code and then execute in Shell.
  • Dispose of your objects. Close the command window by typing Exit.
  • Test before using in Production
  • Write reusable scripts.

What Powershell beginners should avoid

  • Rewriting your variables
  • Hard coding your scripts such as Password as it may get fired by PowerShell
  • Taking code from the internet or vendor and just Run in your environment (You should read every code before you run it in your environment).
  • Assuming the code is not harmful; it is. There is no back button in PowerShell and you cannot undo things.
  • Running your code in an IDE/ISE and expect everything to work.

PowerShell Syntax and Bracketology

Syntax

  • ‘#’ is for Comment
  • ‘+’ is for Add
  • ‘=’, ‘-eq’, are for Equal
  • ‘!’, ‘-ne’, ‘-not’ are for ‘not equal’

Brackets

  • ‘()’ Curved brackets also known as Parentheses are used for required options, compulsory arguments, or control structures.
  • ‘{}’ Curly brackets are used for block expression within a command block and is also used to open a code block
  • ‘[]’ Square brackets are used to denote optional elements or parameters and also used for match functions.

Now that you know the basics of PowerShell, you can start performing key admin tasks on Windows Server 2019.

To further learn how to employ best practices for writing PowerShell scripts and configuring Windows Server 2019 and leverage PowerShell to automate complex Windows server tasks, check out our book, Windows Server 2019 Automation with PowerShell Cookbook – Third Edition written by Thomas Lee.

Read Next

Weaponizing PowerShell with Metasploit and how to defend against PowerShell attacks [Tutorial]

Scripting with Windows Powershell Desired State Configuration [Video]

Automate tasks using Azure PowerShell and Azure CLI [Tutorial]