Automate Your Work Load: Powershell Basics

ReginaOfTech
5 min readJan 14, 2020

--

With automation being at the forefront of technology knowing what is used and how to use it is essential in keeping up with the times. As more and more tasks are needed to maintain and scale business infrastructure, it’s no surprise that automation has become a hot topic. PowerShell has become the ace in the hole when it comes to system automation. It assists you in creating an environment that is efficient and beneficial for your time and, to be honest, sanity. Allowing you to hand off the mundane tasks that take you away from projects that you want to work on. Let’s dive in and understand the basics of PowerShell.

What is PowerShell and Why Should I use it?

It’s an object-oriented automation and scripting language built on .NET. PowerShell can be used on Linux, macOS, and Windows. This task-based command-line allows system administrators and developers to rapidly automate tasks that manage operating systems and processes. This can free up time for developers or admins due to complex tasks being handed off to a program.

Because it is an object-based language, the output from the command can be pipelined to another command. Pipelining is not a new concept and is similar to other shells pipeline. However, instead of text, it is an object being sent. This allows for two separate programs to interact without much effort on the developer’s part.

PowerShell is also great for manipulation server and workstation elements. From backing up a database to allocating memory for a virtual machine. With having access to all of those parts of your computer, running PowerShell is more secure than VBScript, Command Prompt, and other scripting languages.

What is the difference between Powershell and a Command Prompt?

Basically horsepower. Command prompts use, obviously, commands in order to perform. These commands are basic, similar to the GUI on your desktop. Double-clicking on an image in order to move around the system. Powershell is able to handle robust commands due to cmdlets (pronounced command-lets).

Cmdlets are not stand-alone executables like the commands in the command prompt. Instead, they are instances of a .NET framework class. This is how Powershell is able to pass along objects instead of text. Users are able to create single function scripts to harness the power of PowerShell. The script can start and stop a service, retrieve data, and manage event logs.

How Can I Write PowerShell Scripts?

There are two options when it comes to creating a PowerShell script:

  1. Using a notepad and saving it as a .ps1 file
  2. Using the PowerShell Integrated Scripting Environment (ISE)

Option 1 is great for if you are a PowerShell veteran or you just have less than 5 lines that you are confident work without testing. Option 2 is for those who enjoy testing their code no matter how trivial the call is. I’m in the option 2 camp. Definitely taking the time to make sure that the script works in a controlled environment instead of in the free-standing console is a great habit to get into.

PowerShell ISE is made up of 3 components:

  1. Script Interface
  2. PowerShell Console
  3. Command Module

Script Interface is where the commands will go that make up the script. The PowerShell Console is a stand-in command console that allows you to test your script and see if it will play nicely piping it into other scripts if that is the end goal. Command Module is a great reference point to find the command that you are needing. When you select the command at the bottom of the window an insert button will appear. This is great to avoid typos and you end up going down a rabbit hole figuring out what went wrong when you find out an hour later that it was a typo. Yeah, definitely guilty of that happening.

Setting Up Your Computer

Before we dive into the first script we need to check our computers’ execution policy. To do this you will need to open a windows PowerShell in administrator mode. Once the console is up we will do the following:

  1. get-executionpolicy
  2. set-executionpolicy unrestricted
  3. hit the ‘Y’ button to give permission
  4. get-executionpolicy

Get-executionpolicy will retrieve what execution policy the computer is currently set at. If on step 1 it came back as unrestricted you are good to go running scripts. However, if restricted as shown in the console you will need to continue with the above steps. The final get-executionpolicy is to make sure that everything happened correctly and the execution policy is set to unrestricted.

Writing A Script

Let’s write the first script. This is easy and will write on the console “Hello World”. In the ISE in the script console type:

Write-Host "Hello World!"

There is no need for a semi-colon or end symbol. Within the ISE, hit F5 to run the script.

Creating a script to automate tasks is a great way to improve productivity and remove yourself from mundane tasks. Tasks such as backing up a database, copying files from one directory to another, or adding a user to an ungodly number of computers. Saving your seconds and minutes throughout the day will leave you plenty of time to work on a project that you actually want to do.

Common PowerShell Commands

Here is a list of 11 commonly used PowerShell cmdlets:

  1. Get-Help: Displays information about PowerShell commands and concepts.
  2. Set-Location: Sets the current working location to a specified location.
  3. Copy-Item: Copies an item from one location to another.
  4. Start-Job: Starts a Windows PowerShell background job.
  5. Invoke-WebRequest: Gets content from a web page on the internet.
  6. Resume-Job: Restarts a suspended job.
  7. Set-Item: Changes the value of an item to the value specified in the command.
  8. New-Item: Creates a new item.
  9. Remove-Item: Deletes the specified items.
  10. Suspend-Job: Temporarily stops workflow jobs.
  11. Start-Service: Start one or more processes on the local computer.

There are plenty more cmdlets to be learned and used. Check out the documents that Microsoft provides to learn about other commonly used cmdlets. You may even find some powerful, but not so commonly used cmdlets that will benefit you.

Until we learn again,

--

--

ReginaOfTech
ReginaOfTech

Written by ReginaOfTech

A tech explorer with the drive to learn, apply, and expand her mind | https://www.reginaoftech.com

No responses yet