IT professionals wear many hats. Whether they are addressing a helpdesk ticket to reset a user’s password, installing patches on servers or deploying software to their customers, they are responsible for a lot. Fortunately, with a tool called PowerShell at their disposal, IT pros can automate tasks, potentially reducing the time they spend on these tasks by hundreds of hours a year.
Installing PowerShell Core
In this article, we’re going to cover how to get started with PowerShell v6.2 (specifically on Windows) and how it can be applied to an IT pro’s daily life to get back her much-needed time.
PowerShell 6.2 Tutorial for Beginners covers how to get started with PowerShell v6.2 and how it can be applied to an IT Pro’s daily life. Download your copy today for expert knowledge on:
- Installing PowerShell
- Discovering PowerShell
- The PowerShell Pipeline
- Writing Your First Script
Content Summary
Introduction
Installing PowerShell
Discovering PowerShell
Using Get-Command
Using the help system
Objects
The PowerShell pipeline
Writing your first script
Summary
Introduction
IT professionals (pros) wear many hats. Whether they are addressing a helpdesk ticket to reset a user’s password, installing patches on servers or deploying software to their customers, they are responsible for a lot. Fortunately, with a tool called PowerShell at their disposal, IT pros can automate tasks, potentially reducing the time they spend on these tasks by hundreds of hours a year.
PowerShell is a scripting language from Microsoft that has come pre-installed on all Windows systems since Windows 7 and Windows Server 2008 R2. It allows users to automate a nearly infinite number of tasks and enables IT pros to write scripts that replace pointing and clicking with a mouse with merely running a PS1 file.
Imagine never having to run through the New User Wizard in Active Directory, remember to fill out each field just right or read a 30-page Word document to install a critical line of business application only to inevitably miss one crucial step. With PowerShell, these and many other tasks can be automated down to a few simple keystrokes.
In this white paper, we’re going to cover how to get started with PowerShell v6.2 (specifically on Windows) and how it can be applied to an IT pro’s daily life to get back her much-needed time
Installing PowerShell
PowerShell comes pre-installed on any modern Windows operating system, so installation is not needed. Why then are we dedicating a section to installation? To be more specific, the version of PowerShell that comes pre-installed on Windows is called Windows PowerShell. However, as of version 6, Microsoft has ceased development efforts on Windows PowerShell, instead focusing on its open-source successor, PowerShell Core.
Because PowerShell Core is the future, this is where anyone just getting started with PowerShell should start.
Assuming a Windows platform of Windows 7/Windows Server 2008 R2 or higher, there are a few different ways to install PowerShell Core on Windows, but the easiest route is through the PowerShell GitHub repository. To download PowerShell, find the MSI link on the main page for the intended platform of Windows (x64 or x86) and download the MSI.
Once downloaded, run the MSI, which will then present the standard wizard we are all used to seeing.
Run through the Setup Wizard until complete. Once done, click on the Launch PowerShell checkbox and then click Finish. Alternatively, go to the Start menu, type PowerShell, find the PowerShell 6 item and click on that.
Once a black box comes up with a prompt starting with PS and a blinking cursor, PowerShell Core is ready for action!
PowerShell Core prompt
Discovering PowerShell
Now that PowerShell Core has been installed, it’s time to see what it can do. One of the best things about PowerShell Core is how easy and intuitive it is to learn. PowerShell Core commands are named as you’d expect with a – or _-_naming convention. Commands like Get-Content, Stop-Service, Start-Process and more allow you to see what action they perform on a particular entity before a single line of documentation is read.
Using Get-Command
To discover all commands available in PowerShell, run the Get-Command command. This command looks for all currently available commands in the current session. Running Get-Command by itself will return all commands, but typically, it’s common to know a little bit about what commands to look for.
For example, perhaps we need to find all commands that manipulate a Windows service. The Get-Command command has a parameter called Noun that only returns commands with a noun of Service, as shown below:
PS> Get-Command -Noun Service
Using Get-Command
The Get-Command command supports multiple ways to find commands. Use PowerShell Core’s parameter tab-completion feature, available on all commands (not just Get-Command), by typing Get-Command – and hitting the Tab key. PowerShell Core will then start cycling through all available parameters for that command.
Using the help system
Once an interesting command is found, the next step is figuring out how it works. Other than running the command and seeing what happens, we can use PowerShell Core’s built-in help system.
PowerShell Core’s help system provides current documentation for commands. The documentation is current because of PowerShell Core’s native ability to download help content from the internet. When running PowerShell Core for the first time, one of the first tasks is to run the Update-Help command. This command downloads all current help content from Microsoft and makes it available in the help system.
To use the help system, run the Get-Help command, followed by the command in which you’re interested. For example, perhaps we notice a command called Set-Service. It’s not entirely clear what this command does to a Windows service. To see what it does, we can run Get-Help Set-Service, which will return some useful information, including a brief synopsis, syntax and a little bit about the parameters the command has.
Finding help content
PowerShell Core’s help content not only contains typical information, like what parameters are available for each command and a brief synopsis of what the command does, but also includes examples and more.
The Get-Help command has parameters that allow users to discover all help content available with the Full parameter or to display all examples available for a command using the Examples parameter.
PS> Get-Help Set-Service -Full
PS> Get-Help Set-Service -Example
Objects
Everything is an object in PowerShell Core. An object is a programming language term that refers to an entity that has properties and methods. Properties describe various attributes of an object, and methods describe things that an object can do.
Objects and object-oriented programming is a deep topic, but in a nutshell, think of an object like a car. A car has various attributes like color, number of tires, engine size, etc. Those are properties. Methods, on the other hand, are what the car can do, for example, drive forward, drive backward, turn, etc. When just getting started with PowerShell Core, you’ll be working with properties much more frequently than methods, so don’t worry about how to use methods for now. We have PowerShell Core commands to take action for us.
Using the Get-Service command as an example, Get-Service retrieves Windows services. Like the majority of PowerShell Core commands, Get-Service returns one or more objects. In this case, each object is represented by a Windows service. Let’s say that we need to determine that the startup type of the Windows update service is running. To do that, we’d run Get-Service -DisplayName ‘Windows Update’, which would return a single object with what looks like three properties: Status, Name and DisplayName.
PowerShell command to display Status, Name and DisplayName.
It looks like PowerShell Core doesn’t return a startup type, but it does. The property is just not displayed. To ensure all property values are displayed, we’d pipe the output of Get-Service to the Select-Object command using the Property parameter and the * wildcard value to display all properties.
PS> Get-Service -DisplayName 'Windows Update' | Select-Object -Property *
Returning all object properties
The Select-Object command manipulates the output and can select specific or all properties to return.
Finally, another essential command is Get-Member. The Get-Member command does not return the property values, but rather all property names and methods on an object, along with the type of object.
Notice below that the object that Get-Service returns is of type System.Service.ServiceController#StartupType, and it has many different properties and methods. Knowing how to determine available properties on objects is essential.
Knowing how to determine available properties on objects is essential.
Once the object property is found, it can then be referenced using dot notation, which is just a fancy term for putting a dot at the end of an object, followed by the property name, to find the property value (as shown below):
PS> $service = Get-Service -DisplayName 'Windows Update'
PS> $service.StartupType Manual
The PowerShell pipeline
As seen above, PowerShell Core has a concept of commands or cmdlets. Cmdlets are executable entities that perform some action (much like methods do on objects). When run, these cmdlets return some output; we’ve covered this. But cmdlets also accept input. We’ve provided input to cmdlets in the form of parameters, but another important concept to understand is how to give input to commands through the pipeline.
The PowerShell Core pipeline allows scripters to pass output from one command directly to the input of another command in one fell swoop. Using the Get-Service example above, perhaps we need to take an extra step and ensure the Windows update service is started. To start a Windows service, we use a Start-Service command, like Start-Service -DisplayName ‘Windows Update’.
Starting a service like this works, but the pipeline can also be used as shown below:
PS> Get-Service -DisplayName 'Windows Update' | Start-Service
Notice that we didn’t provide a parameter to Start-Service. So how did it know to start the Windows update service? Using the pipeline, PowerShell Core passed the object that Get-Service returned and sent it straight to the Start-Service command. It bound the correct information to Start-Service.
The pipeline provides more benefit if you need to perform actions on many objects at once. Although unlikely, you may want to ensure all services on a machine are stopped. Rather than handing each service one at a time, we pass all service objects to the Stop-Service command, as shown below:
PS> Get-Service | Start-Service
We can even take this a step further by only returning the service name and status. The pipeline supports an unlimited number of commands to pipe to one another.
PS> Get-Service | Start-Service -PassThru | Select-Object -Property Name,Status
Writing your first script
Now that we’ve covered the absolute basics, let’s put together a simple script that brings everything we learned together. This script will find all Windows services on a remote Windows Server and start them.
First, create a simple text file called Start-StoppedService.ps1 and open it with Notepad or the PowerShell ISE. That is where you’ll paste in the code we’re covering in this section.
Before writing any PowerShell code, it’s essential to first break down the problem into pieces. To start all stopped services on a Windows Server, we must first find all the services that are stopped. As we’ve discussed, Windows services can be enumerated using the Get-Service command.
Since Get-Service returns all services, we must then figure out a way to limit the output to only stopped services. After tabbing through the available parameters on Get-Service, we found there’s no way to do this with a parameter. Rather, we must use the pipeline to pass all service objects returned from Get-Service to the Where-Object cmdlet. The Where-Object cmdlet is a common cmdlet that allows us to filter objects based on different criteria. In this instance, we’re only allowing the service objects that have a property value of Stopped on the Status property to return.
$services = Get-Service -ComputerName SRV1 | Where-Object {$_.Status -eq 'Stopped'}
Now that we’ve figured out the code to find all stopped services, we need the code to start them. All service objects were stored in a $services variable for re-use.
All stopped services captured in the $services variable now need to be started using the Start-Service command. Copy and paste the code below into the Start-StoppedService.ps1 script and save it.
$services = Get-Service -ComputerName SRV1 | Where-Object {$_.Status -eq 'Stopped'} $services | Start-Service
Now open up a PowerShell Core console and execute the script (as seen below), replacing “” with the folder where the Start-StoppedService.ps1 is located.
& <PathToScript>\Start-StoppedService.ps1
The PowerShell script starts executing each line in your script (just two in this instance), one by one with no interaction. Imagine the possibilities of adding code to copy files, changing registry keys, adding Windows features and just about anything else to this script. With a single text file, we’re able to boil down any complex task into a single plain text file!
Summary
PowerShell is a powerful automation language for many types of people. Whether you’re an IT professional, software developer, database administrator or helpdesk technician, PowerShell is a tool that can save you hours of time and reduce human error through automation.
Once scripters have learned the basics of PowerShell, they’ll be able to take the language as far as needed to automate complex tasks. Due to PowerShell’s ubiquitous presence, especially with Microsoft products and services, scripters can begin building proverbial blocks to create more complex automation solutions over time. This will save time and money and allow them to use their skills and intelligence for the more important creative tasks that humans should be doing.
Source: Veeam Software