• Zine
  • Anonymous
  • Green
  • IT
  • Gadgets
  • Tech
  • Cloud
  • Data
  • Network
  • Security
  • Hosting
    • Sunset Host Co
    • Radio Host Co
  • Media
    • WSCA News
    • Sunset Crypto
    • Sustainable Action Now
    • Life.Style Magazine
    • Sunset Daily
    • Sunset Music News
    • Pro Merch Sports News
    • Explore New Jersey
    • Explore NJ News
    • The Zine Weekly
Facebook Twitter Instagram
Friday, May 13
Facebook Twitter Instagram
The Zine Weekly
  • Zine

    PowerShell Tutorial: PowerShell Scripting Basics

    May 13, 2022

    VMware sovereign cloud initiative grows to 14 partners

    May 12, 2022

    Windstream Wholesale details T-Rock Express extension, other new fiber route efforts

    May 12, 2022

    EU CSAM scanning law could outlaw end-to-end encryption

    May 12, 2022

    New Patent-Pending Technology Transforms Diluted Waste Carbon Into Valuable Compounds

    May 12, 2022
  • Anonymous

    Anonymous appears to slam Elon Musk’s ‘left wing bias’ comment with new Twitter post

    May 11, 2022

    FACTBOX-The cyber war between Ukraine and Russia

    May 11, 2022

    CIA Expert Decodes Why Russia Which Even Hacked Into US Power Grids Is Failing In Cyber War In Ukraine

    May 10, 2022

    Anti-War Activists Stage ‘Guerrilla’ Protests on Victory Day

    May 9, 2022

    Deepfakes and espionage, but no cyber apocalypse from Vladimir Putin’s invasion

    May 9, 2022
  • Green

    Emerging hydrogen storage technology could increase energy resilience

    May 11, 2022

    Microgrid demo to lend a helping hand in India’s green energy transition

    May 9, 2022

    California prepares for energy shortfalls in hot, dry summer

    May 7, 2022

    Cryptocurrency’s dirty secret: Energy consumption

    May 5, 2022

    Stellantis pours billions more into Canada, electric

    May 3, 2022
  • IT

    Microsoft corrects Windows zero-day for May Patch Tuesday

    May 12, 2022

    WebCitz.com Announces Plans to Offer Free Web Hosting for Non-Profit Churches

    May 11, 2022

    How will VR collaboration transform meetings?

    May 10, 2022

    New Windows 11 preview makes Microsoft accounts mandatory for (almost) all

    May 9, 2022

    Announcing Reliable VPS Server Hosting Provider with Malaysia, Kuala Lumpur, Teluk Intan based IP – TheServerHost

    May 9, 2022
  • Gadgets

    New Patent-Pending Technology Transforms Diluted Waste Carbon Into Valuable Compounds

    May 12, 2022

    How Apple’s music player transformed an industry

    May 12, 2022

    KuCoin Crypto Exchange Raises $150 Million in Funding, Plans to Launch Crypto Wallet, NFT Site

    May 11, 2022

    New tool shows homeowners and renters the true cost of disasters

    May 10, 2022

    iPhone 12, iPhone 12 Mini on Sale With Up to Rs. 11,910 Discount via Amazon India, Flipkart

    May 9, 2022
  • Tech

    4D composite printing could improve the wings of drones

    May 11, 2022

    How to delete unused styles using VBA in Word

    May 9, 2022

    Best Gas Credit Cards in May 2022

    May 6, 2022

    Eshoo faces rivals from left, right and center in bid to retain Congress seat | News

    May 4, 2022

    Netflix cancels Meghan Markle animated series Pearl

    May 2, 2022
  • Cloud

    VMware sovereign cloud initiative grows to 14 partners

    May 12, 2022

    BMW looks to quantum computers to speed car design • The Register

    May 12, 2022

    Alkira launches free tool to tame cloud bloat

    May 11, 2022

    Ministry working to mitigate Merauke-Timika sea cable disruption

    May 10, 2022

    Cisco releases its Cloud Controls Framework to the public

    May 10, 2022
  • Data

    Windstream Wholesale details T-Rock Express extension, other new fiber route efforts

    May 12, 2022

    Asia-Africa-Europe-1 submarine cable system to add Infinera’s ICE6

    May 10, 2022

    Russia’s invasion of Ukraine could hurt Europe IT outsourcing

    May 10, 2022

    Insider Tips for Automating Analytics

    May 9, 2022

    CSPi to Announce Fiscal Second Quarter Financial Results on

    May 8, 2022
  • Network

    PowerShell Tutorial: PowerShell Scripting Basics

    May 13, 2022

    The emerging technologies that shape the financial industry

    May 12, 2022

    Socket lands $4.6M to audit and catch malicious open-source code – TechCrunch

    May 11, 2022

    Keto Start ACV Gummies -WARNING: Shocking Reported About Side Effects? Job – 101 ARW ANG

    May 10, 2022

    Visualization analysis of sEMG | JPR

    May 10, 2022
  • Security

    Otago University bomb threat: Woman sent hoax to hide failure from parents

    May 12, 2022

    There’s Now A Hack To Get Apple CarPlay & Android Auto On Your Tesla

    May 11, 2022

    Alberta Court of Appeal to rule whether federal assessment law is unconstitutional

    May 10, 2022

    Manchin says he’d pass parts of Biden’s agenda. But Democrats may have to write the bill for him.

    May 8, 2022

    Work from home hack to make your cat think it can distract you, is a must-watch | Trending

    May 8, 2022
  • Hosting
    1. Sunset Host Co
    2. Radio Host Co
    Featured

    EU CSAM scanning law could outlaw end-to-end encryption

    By Sunset Host CoMay 12, 20220
    Recent

    EU CSAM scanning law could outlaw end-to-end encryption

    May 12, 2022

    EU Plan to Scan Private Messages for Child Abuse Images Puts Encryption at Risk

    May 12, 2022

    Ransomware attack prompts response from Oregon election officials

    May 11, 2022
  • Media
    1. WSCA News
    2. Sunset Crypto
    3. Sustainable Action Now
    4. Life.Style Magazine
    5. Sunset Daily
    6. Sunset Music News
    7. Pro Merch Sports News
    8. Explore New Jersey
    9. Explore NJ News
    10. The Zine Weekly
    Featured
    Recent

    PowerShell Tutorial: PowerShell Scripting Basics

    May 13, 2022

    VMware sovereign cloud initiative grows to 14 partners

    May 12, 2022

    Windstream Wholesale details T-Rock Express extension, other new fiber route efforts

    May 12, 2022
The Zine Weekly
You are at:Home » PowerShell Tutorial: PowerShell Scripting Basics
Network

PowerShell Tutorial: PowerShell Scripting Basics

Scott MattesonBy Scott MattesonMay 13, 2022No Comments9 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
Share
Facebook Twitter LinkedIn Pinterest Email
Illustration: Lisa Hornung

Microsoft PowerShell, generally referred to as Windows PowerShell, offers a handy way to automate various chores whether you’re working on a Windows Server or a Windows workstation.

System administrators would do well to learn this scripting language as a skill with which to automate tasks – particularly repetitive tasks – and develop advanced administrative tasks to help lighten their workloads and execute operations via predictable, proven commands. These commands can be run on local systems or remote ones using the PowerShell remoting function, for example, to make updates to the Windows registry on multiple systems.

SEE: Feature comparison: Time tracking software and systems (TechRepublic Premium)

More about Software

PowerShell isn’t a programming language like Visual Studio Code aka VS Code so much as a scripting language. PowerShell variables, cmdlets, modules and providers are the foundation and constitute powerful PowerShell tools to help get the job done.

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

Windows PowerShell, which runs on the Windows OS exclusively, is augmented by PowerShell Core, which is an advanced version capable of running on the operating systems Windows, Mac and Linux in the same manner Visual Studio Code does.

Here is a Windows PowerShell scripting tutorial intended as a beginner’s guide to illustrate scripting basics to help beginners as they start working with existing Windows PowerShell scripts or PowerShell cmdlets or building new ones.

You can launch Windows PowerShell from the Start Menu under the Windows PowerShell folder in order to orient yourself 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 which appears as follows:

I prefer using Windows PowerShell ISE for PowerShell scripting since it provides a PowerShell console window to permit command prompt functionality along with a handy toolbar ribbon and command references listed on the right which you can click to insert PowerShell commands as well as learn more about them.

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 purposes.

1. PS1 files

PowerShell syntax can be a bit daunting for the newcomer, so let’s start with the basics of scripts which are also called PS1 files. A Windows PowerShell script is really nothing more than a simple text file that can be run in either Windows PowerShell or Windows PowerShell ISE. The PowerShell scripting language operates by executing a series of PowerShell commands (or a single one), with each command appearing on a separate line. For the text file to be treated as a PowerShell script, its filename needs to end in .PS1 to connote 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 provide you output similar to the following:

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

2. Execution permissions

To prevent the execution of malicious scripts, PowerShell enforces an execution policy. By default, the execution policy is set to Restricted, 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:

  • Restricted–Scripts won’t run.
  • RemoteSigned–Scripts created locally will run, but those downloaded from the internet will not (unless they are digitally signed by a trusted publisher).
  • AllSigned–Scripts will run only if they have been signed by a trusted publisher.
  • Unrestricted–Scripts will run regardless of where they have come from and whether they are signed.

You can set PowerShell’s execution policy by using the following cmdlet:

Set-ExecutionPolicy <policy name>

Note: when typing in the PowerShell command prompt you can enter part of the command and hit Tab to autofill the rest (or show multiple choices matching what you’ve put in). For instance, typing Set-Ex and pressing tab will autofill the entire Set-ExecutionPolicy command and save you some time.

3. Running a script

For years now, if you wanted to run an executable file from the command line the practice was to navigate to the file’s path and then type the name of the executable file. However, this age-old method doesn’t work for PowerShell scripts.

If you want to execute a PowerShell script, you will usually have to type the full path along with the filename. For example, to run a script named SCRIPT.PS1, you might type:

C:ScriptsScript.ps1

The big exception is that you can execute a script by simply typing its name if the folder containing the script is in your system’s path. There is also a shortcut you can use if you are already in the folder containing the script. Instead of typing the script’s full path in such a situation, you can enter . and the script’s name. For example, you might type:

.Script.ps1

4. Pipelining

Pipelining is the term for feeding one command’s output into another command. This allows the second command to act on the input it has received. To pipeline two commands (or cmdlets), simply separate them with the pipe symbol (|).

To help you understand how pipelining works, imagine that you want to create a list of processes that are running on a server and sort that list by process ID number. You can get a list of processes by using the Get-Process cmdlet, but the list will not be sorted. However, if you pipeline the cmdlet’s output into 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 pipelining to feed one command’s output into another command, sometimes pipelining alone won’t get the job done. When you pipeline a command’s output into another command, that output is used immediately. Occasionally, you may need to store 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 a command’s full output. For example, suppose you want to store the list of processes running on a server as a variable. To do so, you could use this line of code:

$a = Get-Process

Here, the variable is named $a. If you want to use the variable, simply call it by name. For example, typing $a prints the variable’s contents on the screen.

You can assign a variable to the final output of multiple commands that have been pipelined together. Just surround the commands with parentheses. For example, to sort the running processes by process ID and then assign the output to a variable, you could use this command:

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

Running “echo $a” will then execute 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, take the following line of code, which creates a variable named $Procs that contains multiple lines of text (an array):

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

You can also use the @ symbol when the variable is used, to ensure that it is treated as an array rather than a single value. For instance, the line of code below will run the Get-Process cmdlet against the variable I defined a moment ago. In doing so, Windows will display all the processes used by Windows Explorer and Svchost. Notice how the @ symbol is being used in front of the variable name rather than the dollar sign that we usually see used:

Get-Process @procs

7. Split

The split operator splits a text string based on a character you designate. For example, suppose that you want to break a sentence into an array consisting of each individual word in the sentence. You could do so by using a command like this one:

"This is a test" -split " "

The result would look like this:

This

is

a

test

8. Join

Just as split can split a text 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 quotation marks 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 bugs. One way to protect yourself is to insert breakpoints at strategic locations within your script. That way, you can make sure that the script is working as intended before you process the entire thing.

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

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

You can also bind a breakpoint to a variable. So, if you wanted your script to break any time the contents of a$ changed, you could use a command like this one:

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

Notice that I didn’t 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, it may sometimes be necessary to run the script line by line. To do so, you can use the Stepping process in PowerShell ISE to cause the script to pause after each line regardless of whether a breakpoint exists. Utilize the functions outlined in the table below.

Image: Microsoft

Share this:

  • Twitter
  • Facebook

Related

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleVMware sovereign cloud initiative grows to 14 partners
Scott Matteson

Related Posts

The emerging technologies that shape the financial industry

May 12, 2022

Socket lands $4.6M to audit and catch malicious open-source code – TechCrunch

May 11, 2022

Keto Start ACV Gummies -WARNING: Shocking Reported About Side Effects? Job – 101 ARW ANG

May 10, 2022

Comments are closed.

Categories
  • Anonymous (92)
  • Cloud (147)
  • Data Center (129)
  • Gadgets (168)
  • Green Tech (37)
  • Hosting solutions (91)
  • IT News (91)
  • Network (137)
  • Security (129)
  • Tech (95)
  • Web hosting (58)
  • Zine
  • Anonymous
  • Green
  • IT
  • Gadgets
  • Tech
  • Cloud
  • Data
  • Network
  • Security
  • Hosting
    • Sunset Host Co
    • Radio Host Co
  • Media
    • WSCA News
    • Sunset Crypto
    • Sustainable Action Now
    • Life.Style Magazine
    • Sunset Daily
    • Sunset Music News
    • Pro Merch Sports News
    • Explore New Jersey
    • Explore NJ News
    • The Zine Weekly

Subscribe to Updates

Get the latest tech news from the Zine at the Sunset Host Co. and the Radio Host Co.

PowerShell Tutorial: PowerShell Scripting Basics

May 13, 2022

VMware sovereign cloud initiative grows to 14 partners

May 12, 2022

Windstream Wholesale details T-Rock Express extension, other new fiber route efforts

May 12, 2022

EU CSAM scanning law could outlaw end-to-end encryption

May 12, 2022

New Patent-Pending Technology Transforms Diluted Waste Carbon Into Valuable Compounds

May 12, 2022

Microsoft corrects Windows zero-day for May Patch Tuesday

May 12, 2022

Otago University bomb threat: Woman sent hoax to hide failure from parents

May 12, 2022

The emerging technologies that shape the financial industry

May 12, 2022
Copyright © 2022. The Zine Weekly, an SCA Entertainment & Media Company. All Rights Reserved.

Type above and press Enter to search. Press Esc to cancel.