Microsoft PowerShell, commonly referred to as Windows PowerShell, provides a convenient way to automate various tasks, whether you’re on a Windows Server or a Windows workstation.
System administrators would do well to learn this scripting language as a skill that allows them to automate tasks – especially repetitive tasks – and develop advanced administrative tasks to lighten their workload and perform operations through predictable, proven commands. These commands can be run on local or remote systems using the PowerShell remoting function, for example to make updates to the Windows registry on multiple systems.
SEE: Feature Comparison: Time Attendance Software and Systems (Tech Republic Premium)
PowerShell is not so much a programming language like Visual Studio Code, aka VS Code, but rather a scripting language. PowerShell variables, cmdlets, modules, and providers are the foundation and are powerful PowerShell tools to get the job done.
- A variable is a term to identify a specific value that can be easily referenced and reused.
- A cmdlet is a built-in PowerShell command (the Get command cmdlet is one of the most common with multiple uses, such as retrieving existing settings or seeking help via the get-help cmdlet).
- A module is a package that contains multiple objects, such as cmdlets, variables, and more.
- A provider is a .NET program that provides access to information such as the registry, aliases, functions, variables, file systems, and the overall operating system environment.
Windows PowerShell, which runs exclusively on the Windows operating system, is complemented by PowerShell Core, an advanced version that runs on Windows, Mac, and Linux operating systems in the same way as Visual Studio Code.
Here is a Windows PowerShell scripting tutorial intended as a beginner’s guide to illustrate scripting basics to help beginners work with existing Windows PowerShell scripts or PowerShell cmdlets or build new ones.
You can start Windows PowerShell from the Start menu under the Windows PowerShell folder to get oriented on how to use this PowerShell tutorial. The two programs to work with are Windows PowerShell and Windows PowerShell ISE (Integrated Scripting Environment). The first executable is the Command Prompt interface and the second is a GUI based interface that looks like this:
I prefer to use Windows PowerShell ISE for PowerShell scripting, as it provides a PowerShell console window to enable command prompt functionality, along with a handy toolbar ribbon and command references on the right that you can click to insert and learn more about PowerShell commands. to find out.
Note that the default executables are 64-bit, but a 32-bit PowerShell version of each can be found in this folder for backwards compatibility.
1. PS1 Files
PowerShell syntax can be a little daunting for the newbie, so let’s start with the basics of scripts also called PS1 files. A Windows PowerShell script is really nothing more than a simple text file that can be run in Windows PowerShell or Windows PowerShell ISE. The PowerShell scripting language works by executing a series of PowerShell commands (or a single one), with each command listed on a separate line. To treat the text file as a PowerShell script, the filename must end in .PS1 to denote a PowerShell extension.
The simplest, most basic PowerShell example is a file called Datecheck.ps1, which contains the following entry:
Get-Date
Running this will give you an output similar to the following:
Tuesday, May 10, 2022 3:20:04 pm
2. Performance rights
To prevent the execution of malicious scripts, PowerShell enforces an execution policy. The execution policy is set to Restricted by default, which means that PowerShell scripts will not run. You can determine the current execution policy by using the following cmdlet:
Get-ExecutionPolicy
The execution policies you can use are:
- Limited–Scripts are not executed.
- Signed remotely–Scripts created locally will run, but scripts downloaded from the Internet will not (unless digitally signed by a trusted publisher).
- All signed–Scripts will only run if they are signed by a trusted publisher.
- Unlimited–Scripts run regardless of where they come from and whether they are signed.
You can set the PowerShell execution policy using the following cmdlet:
Set-ExecutionPolicy <policy name>
Note: When you type the PowerShell Command Prompt, you can enter part of the command and press Tab to autofill the rest (or show multiple choices that match what you entered). For example, typing Set-Ex and pressing tab automatically populates the entire Set-ExecutionPolicy command, saving you time.
3. Run a script
For years, if you wanted to run an executable from the command line, it was common to navigate to the file’s path and then enter the name of the executable. However, this age-old method doesn’t work for PowerShell scripts.
To run a PowerShell script, you usually have to type the full path along with the file name. For example, to run a script called SCRIPT.PS1, type:
C:\Scripts\Script.ps1
The big exception is that you can run a script by simply typing its name if the directory containing the script is in your system’s path. There is also a shortcut you can use if you are already in the script folder. Instead of typing the full path of the script in such a situation, you can enter .\ and the name of the script. For example, you can type:
.\Script.ps1
4. Pipelines
Pipelining is the term for feeding the output of one command into another command. This allows the second command to respond to the input it has received. To pipe two commands (or cmdlets), simply separate them with the pipe (|) symbol.
To help you understand how pipelining works, imagine you want to create a list of processes running on a server and sort that list by process ID number. You can get a list of processes using the Get-Process cmdlet, but the list is not sorted. However, if you pipeline the output of the cmdlet into the Sort-Object ID command, the list will be sorted. The sequence of commands used looks like this:
Get-Process | Sort-Object ID
5. Variables
While you can use pipelining to direct the output from one command to another, sometimes pipelining alone won’t get the job done. When you pipe the output from one command to another command, that output is used immediately. Occasionally you may need to save the output for a while so that you can use (or reuse) it later. This is where a PowerShell variable can come into play.
It’s easy to think of a variable as a repository for storing a value, but in PowerShell, a variable can store the entire output of a command. Suppose you want to store 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 called $a. If you want to use the variable, just call it by name. For example typing $a prints the contents of the variable on the screen.
You can assign a variable to the final output of multiple commands that are pipelined together. Just put the commands in parentheses. For example, if you want to sort the 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” executes the command you assigned to the variable.
6. The @ Symbol
By using the @ symbol, you can turn the contents of a list into an array. For example, consider the following line of code, which creates a variable called $Procs containing multiple lines of text (an array):
$procs = @{name="explorer","svchost"}
You can also use the @ symbol when the variable is used to ensure it is treated as an array rather than a single value. For example, the line of code below runs the Get-Process cmdlet against the variable I just defined. In addition, Windows lists all processes used by Windows Explorer and Svchost. Notice how the @ symbol is used for the variable’s name instead of the dollar sign we commonly see used:
Get-Process @procs
7. Split
The split operator splits a string based on a character you point to. For example, suppose you want to split a sentence into an array made up of every single word in the sentence. You could do this by using a command like this:
"This is a test" -split " "
The result would look like this:
This one
is
a
test
8. Get involved
Just as split can split a string into multiple pieces, the join operator can combine multiple blocks of text into one. For example, this line will create a text string consisting of my first name and last name:
"Scott","Matteson" -join " "
The space 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 has bugs. One way to protect yourself is to insert breakpoints in strategic locations in your script. That way you can make sure the script works as intended before processing the whole thing.
The easiest way to insert a breakpoint is by line number. For example, if you want to insert a breakpoint on the 10th line of a script, you can use a command like this:
Set-PSBreakpoint -Script C:\Scripts\Script.ps1 -Line 10
You can also bind 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:\scripts\Script.ps1 -variables a
Note that I have not included 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, it may sometimes be necessary to run the script line by line. To do this, you can use the stepping process in PowerShell ISE to ensure that the script breaks after each line, regardless of whether a breakpoint exists. Use the described functions in the table below†
