Powershell Scripting Basics

13 Mar 2021 9039 views 0 minutes to read Contributors

Introduction

Windows PowerShell is a powerful tool for automating tasks and simplifying configuration and can be used to automate almost any task in the Windows ecosystem.

 

If this is your first time working with PowerShell scripts, this might not work. That’s because there is a system-wide policy that prevents execution. Run this command in PowerShell:

Get-ExecutionPolicy

Powershell execution policy 

 

You will see one of the following outputs:

  • Restricted— No scripts will be executed. This is the default setting in Windows, so you’ll need to change it. 
  • AllSigned— You can only run scripts signed by a trusted developer. You will be prompted before running any script.
  • RemoteSigned— You can run your own scripts or scripts signed by a trusted developer. 
  • Unrestricted— You can run any script you want. This option should not be used, for obvious reasons.

To start working with PowerShell scripts, you’ll need to change this policy setting. You should change it to ‘RemoteSigned’, and you can do that right from PowerShell by running the following command:

Set-ExecutionPolicy RemoteSigned

 

Now you are ready to get started.

Powershell Command

You have different commands, cmdlets, flags, filters and other ways of telling PowerShell what to do.

And its not feasible to remember all, in this case, you’d use a command for finding other commands.

 

Get-Command 

 

In searching for commands, it’s important to keep in mind that there’s a syntax to them: VERB-NOUN. Typically the verbs are things like Get, Set, Add, Clear, Write and Read and the Nouns are the files, servers, or other items within your network and applications.

Get-Command is a discovery tool for exploring the commands available on your system.

Command discovery

 

 

How to run a powershell script

 

There are below two ways via which we can execute scripts,

  1. Have a script created and saved as a powershell script file (.ps1), then we can execute in powershell console window using command & "D:\SQLT\Scripts\ScriptExecution.ps1" as is shown below.

 

  1. The second, much more powerful way of making PowerShell scripts is to use the Windows PowerShell Integrated Scripting Environment (ISE). With ISE, you can run scripts and debug them in a GUI environment. 

 

 

 

Basic PowerShell Script Examples

Now you can start to write PowerShell scripts. So let’s go through the process step by step.

  • Get The Date
  • Force Stop Processes
  • Check if a File Exists

 

Example Script 1: Get The Date

Let’s start with a simple script. In ISE or notepad, open a new file. Type:

Get-Date

And then save the file as GetDate.ps1

You can call the script from PowerShell using the command:

& "D:\SQLT\Scripts\GetDate.ps1"

 

 

 

Force Stop a process

Before we stop a process we should have the information of the  process we want to stop. We can get this information from using below command,

Get-Process -ProcessName Skype

Using this command we will get running processes details for Skype application, as shown below.

 

To do that, make a new script file in the same way as before. This time, type:

stop-process 8836

or

stop-process -processname Skype

Save the file as StopSkype.ps1, and then you can invoke the script using:

& " D:\SQLT\Scripts\StopSkype.ps1"

This script can be expanded to stop a number of processes at once, just by adding extra commands of the same type. You can also write another script if you want to automatically start a number of processes at once, using:

start-process -processname [your process here]

This is particularly useful if you want to start a bunch of networking processes at once, for instance, and don’t want to enter the commands separately.

 

Example Script 3: Check if a File Exists

Suppose you need to delete multiple files, you might want to first check to see if the files even exist.

test-path, as the name implies, lets you verify whether elements of the path exist. It’ll return TRUE, if all elements exist, and FALSE if any are missing.

You simply type:

test-Path (And then the file path)

 

PowerShell Punctuation

Here’s a table to summarize some of the PowerShell punctuation we’ve used:

Symbol

Name

Function

Example

$

Dollar Sign

Declares a variable

$a

=

Equal

Assigns a value to the variable

$a=get-date

“”

Double Quote

Use double quotes to display text

If $a = Monday

“Day of the Week: $a”
will output as:

Day of the Week: Monday

+

Plus

Concatenates

$a = November

“Day of the Week: ”
+ $a.Dayofweek

Day of the Week: Monday

( )

Parenthesis

Groups to create argument

 (get-date).day

 

Report a Bug

In this article