- Docs
- /
13 Mar 2021 9140 views 0 minutes to read Contributors
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
You will see one of the following outputs:
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.
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.
There are below two ways via which we can execute scripts,
Now you can start to write PowerShell scripts. So let’s go through the process step by step.
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"
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.
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)
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” Day of the Week: Monday |
+ |
Plus |
Concatenates |
$a = November “Day of the Week: ” Day of the Week: Monday |
( ) |
Parenthesis |
Groups to create argument |
(get-date).day |
In this article
Please choose a monitoring environment to connect to:
Enter your email address to recover your password.
Download link successfully sent. Check your email for details.
Reset password link successfully sent to . Check your email for details.
An email with a link for creating your password is sent to
Share link successfully sent to .
Your reply is send.
Download link successfully sent to . Check your email for details.
Download link successfully sent to . Check your email for details.
Download link successfully sent to . Check your email for details.
Download link successfully sent to . Check your email for details.
We received your request. We will contact you shortly