Share on facebook
Share on twitter
Share on linkedin

Powering Up with PowerShell: Put the Awesomeness in Action

Landon Fowler
Landon Fowler

There are few things more fun than automating tasks. (Don’t try to think up things that are more fun. Hear me out . . .) Automation is like a dream come true for someone who spent childhood obsessed with robot toys and cartoons. Really, I never grew out of thinking robots are awesome, but I have grown to appreciate the satisfying fun that comes from automating tasks with scripting languages like PowerShell.

We may not have android butlers to fetch our coffee or mechs that can rocket us over rush hour, but we can get a taste of a more efficient, automated future right now with PowerShell. And it’s easier than you might think.

An intro to PowerShell

If you’re new to PowerShell, or to scripting in general, it can be tough to know where to get started. You see the potential, you want to automate ALL THE THINGS, but how do you get started when you don’t know what you don’t know?

Consider this your intro to PowerShell. This is PowerShell for beginners — PowerShell 101. I’ll run you through some tips on getting started with PowerShell and share some resources so you too can discover the satisfaction of automation.

The New Kid on the Block

First of all, what is PowerShell and what makes it different?

Simply put, it’s a scripting language and command-line shell that’s made by Microsoft. Though it started as a Windows exclusive, it’s now cross-platform and runs on Linux, macOS, and Windows.

But there are a number of really good scripting languages already available, so why choose PowerShell?

First, its syntax is well laid out and consistent across a variety of tasks, making it easy to learn and use effectively. Second, unlike most utilities, its output is objects rather than text, allowing you a greater level of interaction between commands. Lastly, since it’s now cross-platform, you can write one script to rule them all even if you have an environment of mixed operating systems (and most do).

OK. Now, let’s say you’ve bought into the awesomeness that is PowerShell and are ready to get started. You’ve installed it, launched it, and are now staring blankly at the screen wondering what on earth to do first. Let’s give you a compass of sorts to help make sure your journey into PowerShell is a pleasant and successful one.

Finding Commands

The first thing to know with PowerShell is that its commands (or cmdlets) are all structured in a verb-noun format. So, if I’m wanting to get a list of commands, then I can intuitively know to use Get-Command. This will pull back for me a list of all the commands available on my system.

As your PowerShell skill improves you can further sort this list too — for example, get all of the commands of a specific module. But more on that in a moment.

Getting Help

A list of commands is great, but only if you know what to do with them. Since help is what we need to get, we can again know through PowerShell’s well-thought-out syntax that Get-Help is what we need to run.

Use that along with one of the cmdlet names, as shown below, and you’ll get information ranging from syntax definitions to full examples. The default is to return basic information, but be sure to add the -full flag to return everything.

Get-Help Get-Process -full

Also, remember to not overcomplicate things. Our natural instinct is to Google our questions, whether it’s scripting syntax or how to patch drywall. Follow your instincts!

Microsoft has fantastic documentation on all their cmdlets online, and simply Googling the cmdlet name will get you there fast. After more than a decade of using PowerShell, I do the same thing — so no shame!

Exploring PowerShell’s cmdlets

You are now well equipped to start exploring PowerShell’s cmdlets on your own, but let me give you some of my favorite examples to get the creative juices flowing.

Find Cmdlets of a Specific Module

Many times, you are working with a specific product but may have several modules installed. In those instances, it can be difficult to sort through all of the available cmdlets when simply running Get-Command. For those cases, limit your search to a specific module, and even a particular verb! For instance, if I wanted to see all of the “Get-” cmdlets in the Azure SQL module, I could run the following:

Get-Command -Module Az.SQL -Verb Get

Piping Objects For the Win

As mentioned before, one of the stand-out features of PowerShell above other languages is the ability to output objects rather than just plain text.

For example, to list processes on a Linux machine, I’d use the command ps. This certainly works, but what if you need to do something more powerful, such as pipe the output to the kill command? To accomplish that, you’d have to parse the string output using other commands, such as grep and awk. That might look something like this:

kill $(ps -e | grep httpd | awk ‘{print $1}’)

However, since PowerShell outputs objects instead of simply plain text, we can easily reference each process that is listed by Get-Process, as well as the individual elements of those objects. To perform the same action as listed above, we could do something like this:

Get-Process httpd | Stop-Process

This is a much more simple and elegant action because we are passing the information about “httpd” as an object, allowing the cmdlet to which we’re piping to easily discern the pieces of information it needs. The same is true when we set variables equal to cmdlet results, such as this:

$var = Get-Process httpd

$var.Id

In the above example, I set the variable $var equal to the object returned by the cmdlet. I can then reference the Id property specifically just by appending a period and the property name. This becomes very powerful when scripting, allowing you to easily access exactly the information you need without a lot of complicated grep, awk or sed operations.

There’s a Module for That

Another way that PowerShell stands apart from others is its library of modules and the consistency across those modules. Whether I’m scripting against Azure, Active Directory, or SQL Server, there’s a module for that. I can import one or all of them, and use them in conjunction with one another. I can even pull down third party modules from PowerShell Gallery and loop them in as well. A few cool examples are:

Executing SQL Server Agent jobs on a remote instance:

Invoke-Sqlcmd -ServerInstance “SQLINSTANCENAME” -Database MSDB -query “EXEC dbo.sp_start_job N’My_AwesomeJob’;”

Copy AD group memberships from one user to another:

Get-Aduser user1 -Properties memberof | select -ExpandProperty memberof | ForEach-Object -Process {$groupName = ($_ -split ‘,*..=’)[1]; Add-ADGroupMember -Identity $groupName -Members user2}

Enable Windows firewall rule groups:

Enable-NetFirewallRule -DisplayGroup “Remote Service Management”,”Remote Event Log Management”,”Remote Desktop”

Keep It Going

As you can see, there’s a ton of potential with PowerShell. You now know how to find commands, how to get the info needed to use them effectively, and some examples of their power. You’re well on your way to becoming a PowerShell pro!

With these tools in hand, you have a foundation for exploring this simple yet powerful scripting language. Before long you’ll be writing cross-platform scripts in order to automate all the things, whether they run on Windows or Linux.

For more information on getting up and running with PowerShell, check out my mini-course Getting Started With PowerShell. This crash course will quickly take you from the ground up installing, using, and scripting in PowerShell. See you in Shell!

More PowerShell resources

If you know a bit about PowerShell and are ready to learn a LOT about PowerShell and level up your PowerShell abilities, check out some of A Cloud Guru’s other PowerShell courses.

Recommended

Get more insights, news, and assorted awesomeness around all things cloud learning.

Get Started

Who’s going to be learning?

Sign In

Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout