Illustration: Lisa Hornung

Microsoft PowerShell, commonly referred to as Windows PowerShell, offers a convenient way to automate a variety of tasks, whether you’re running Windows Server or a Windows workstation.

System administrators would do well to learn this scripting language as a skill to automate tasks – especially repetitive tasks – and to develop advanced administrative tasks to help ease their workload and perform operations with predictable, proven commands. These commands can be run on local or remote systems, using the PowerShell remote feature, for example, to make Windows registry updates on multiple systems.

LOOK: Comparison of functions: Software and time tracking systems (TechRepublic Premium)

Note that PowerShell is not a programming language like Visual Studio Code (also known as VS Code) as much as a scripting language. PowerShell variables, commands, modules, and vendors are the foundation and powerful PowerShell tools that help get the job done.

  • A variable is a term used to identify a specific value for easy reference and reuse.
  • The cmdlet is a built-in PowerShell command (the Get command is one of the most common with many uses, such as retrieving existing settings or seeking help using the get-help cmdlet).
  • The module is a package containing many objects such as cmdlets, variables, etc.
  • The provider is a .NET program that provides access to information such as registry, aliases, functions, variables, file systems, and the overall operating system environment.

Note that Windows PowerShell, which runs exclusively on Windows, is complemented by PowerShell Core, which is an advanced version that can run on Windows, Mac, and Linux in the same way as Visual Studio Code.

Here’s a Windows PowerShell scripting tutorial designed as a beginner’s guide to illustrate the basics of scripts to help beginners when they start working with existing Windows PowerShell scripts or PowerShell cmdlets or creating new ones.

You can start Windows PowerShell from the Start menu below the Windows PowerShell folder to navigate using this PowerShell tutorial. The two programs you need to work with are Windows PowerShell and Windows PowerShell ISE (Integrated Scripting Environment). The first executable file is the command line interface, and the second is a GUI-based interface that looks like this:

I prefer to use Windows PowerShell ISE for PowerShell scripts because it provides a PowerShell console window that allows command-line functionality, along with a handy toolbar and links to commands listed on the right that you can click to insert PowerShell commands, and learn more about them.

Note that the default executables are 64-bit, but a 32-bit version of PowerShell for anyone can be found in this folder for backward compatibility purposes.

1. PS1 files

PowerShell syntax can be a little daunting for a newcomer, so let’s start with the basics of scripts, also called PS1 files. The Windows PowerShell script is actually nothing more than a plain text file that can run in Windows PowerShell or Windows PowerShell ISE. The PowerShell scripting language works by executing a series of PowerShell commands (or one), with each command appearing on a separate line. To treat a text file as a PowerShell script, its name must end in .PS1 to indicate a PowerShell extension.

The simplest and most basic example of PowerShell is a file called Datecheck.ps1, which contains the following entry:

Get-Date

Doing so will provide you with an outcome similar to the following:

Tuesday, May 10, 2022 3:20:04 pm

2. Permits for implementation

To prevent malicious scripts from running, PowerShell imposes an execution policy. By default, the execution policy is set to Restricted, which means that PowerShell scripts will not run. You can define the current execution policy using the following cmdlet:

Get-ExecutionPolicy

The implementing rules you can use are:

  • Limited– Scripts will not run.
  • RemoteSigned– Scripts created locally will run, but those downloaded from the Internet will not (unless digitally signed by a trusted publisher).
  • AllSigned– Scripts will only run if signed by a trusted publisher.
  • Unlimited– Scripts will run regardless of where they came from or whether they were signed.

You can set the rules for running PowerShell using the following cmdlet:

Set-ExecutionPolicy <policy name>

Note: When typing on the PowerShell command line, you can enter part of the command and press Tab to automatically fill in the rest (or display multiple choices that match what you set). For example, entering Set-Ex and pressing tab will automatically populate the entire Set-ExecutionPolicy command and save you some time.

3. Execute a script

For years, if you wanted to run an executable file from the command line, the practice was to go to the file path and then enter the name of the executable file. However, this age-old method does not work for PowerShell scripts.

If you want to run a PowerShell script, you will usually need to enter the full path along with the file name. For example, to run a script named SCRIPT.PS1, you can write:

C: Scripts Script.ps1

The big exception is that you can run a script by simply typing its name if the folder containing the script is in the path of your system. There is also a shortcut that you can use if you are already in the folder containing the script. Instead of writing the full path of the script in such a situation, you can enter. And the name of the script. For example, you can write:

.Script.ps1

4. Pipelines

Pipelining is the term for passing the output of one command to another command. This allows the second command to act on the input it received. To redirect two commands (or cmdlets), simply separate them with the pipe symbol (|).

To help you understand how the pipeline works, imagine that you want to create a list of processes running on a server and sort that list by process ID. You can get a list of processes using the Get-Process cmdlet, but the list will not be sorted. However, if you translate the cmdlet output to the Sort-Object ID command, the list will be sorted. The string of commands used looks like this:

Get-Process | Sort-Object ID

5. Variables

Although you can use a pipeline to pass the output of one command to another, sometimes pipelining alone will not do the job. When you redirect a command output to another command, that output is used immediately. Sometimes you may need to save the output for a while so that you can use it (or reuse it) later. This is where the PowerShell variable can come into play.

It’s easy to think of a variable as a storage for value, but in PowerShell the variable can store the full output of the command. For example, suppose you want to save the list of processes running on a server as a variable. To do this, you can use this line of code:

$a = Get-Process

Here the variable is named $ a. If you want to use the variable, just call it by name. For example, write $ a prints the contents of the variable on the screen.

You can assign a variable to the end output of multiple commands that have been mounted together. Just enclose the commands with brackets. For example, to sort running processes by process ID and then assign the output to a variable, you can use this command:

$a = (Get-Process | Sort-Object ID)

Executing “echo $ a” will then execute the command you set for the variable.

6. The @ symbol

You can use the @ symbol to turn the contents of a list into an array. For example, take the following line of code that creates a variable named $ Procs that contains multiple lines of text (array):

$procs = @{name="explorer","svchost"}

You can also use the @ symbol when using a variable to ensure that it is treated as an array and not as a single value. For example, the line of code below will execute the Get-Process cmdlet against the variable I just defined. In this way, Windows will show all the processes used by Windows Explorer and Svchost. Notice how the @ symbol is used in front of the variable name instead of the dollar sign we usually see used:

Get-Process @procs

7. Separation

The split operator separates a text string based on the character you specify. For example, suppose you want to break a sentence into an array of each word in the sentence. You can do this using a command like this:

"This is a test" -split " "

The result would look like this:

This

is

a

test

8. Join

Just as a split can split a text string into multiple parts, a link operator can combine multiple blocks of text into one. For example, this line will create a text string consisting of my first and last name:

"Scott","Matteson" -join " "

The spacing between the quotes at the end of the command tells Windows to insert a space between the two text strings.

9. Breakpoints

Running a newly created PowerShell script can have unintended consequences if the script contains errors. One way to protect yourself is to insert breakpoints at strategic locations in your script. This way you can make sure that the script works as intended before processing the whole thing.

The easiest way to insert a breakpoint is through a line number. For example, to insert a breakpoint on the 10th line of a script, you can use a command like this:

Set-PSBreakpoint -Script C:ScriptsScript.ps1 -Line 10

You can also connect a breakpoint to a variable. So if you want your script to break every time the contents of a $ change, you can use a command like this:

Set-PSBreakpoint -Script C:scriptsScript.ps1 -variables a

Note that I did not include the dollar sign after the variable name.

There are a number of verbs you can use with PSBreakpoint, including Get, Enable, Disable, and Remove.

10. Step

When debugging a script, you may sometimes need to run the script line by line. To do this, you can use the step process in PowerShell ISE to cause the script to pause after each line, whether or not there is a breakpoint. Use the described functions in the table below.

Image: Microsoft

PowerShell tutorial: The basics of PowerShell scripting

Previous articleThe first round of this year’s PITR & TR Golf Society starts today – TECHNOLOGICAL TRUSTER
Next articleNIST awards funding to strengthen modern manufacturing for critical and emerging technologies