Ultimate Guide to create and run PowerShell Scripts?


👤 Diwas Poudel    🕒 Sep 13 2021    📁 Fix    📜 0 comment

PowerShell is Microsoft developed powerful tools for task automation and configuration management consisting of excellent command-line shell as well as a scripting language. It is designed for Sys Admin and IT professionals for automating a wide range of time-consuming administrative tasks. Powershell is used by developers to test build and deploy solutions in CI/CD environments.

Powershell is initially developed as a scripting language only for Windows and later available for other OS as well. Initially, it was built on .Net Framework which includes Powershell 1.0, 2.0,3.0, 4.0,5.0, and 5.1. Later it was built on .Net Core Framework on 18 August 2016  and due to the cross-platform nature of the .net core PowerShell is also cross-platform and open source and released with the new name PowerShell Core.

So now you can run Powershell in Windows, several latest distributions of Linux and MAC.

What is PowerShell Script?

Powershell scripts are series of PowerShell commands written in a text file with a .ps1 extension and these scripts are executed sequentially when a call to the script is made. Like other .exe files, .bat files, image files you cannot run PowerShell script files by double-clicking it. To run Powershell scripts you need to follow the following steps.

  1. Powershell Installed in your machine
  2. Enable execution of PowerShell scripts.
  3. Create the scripts.
  4. Run the scripts

1 Powershell Installed in your machine

Powershell comes preinstalled in your Windows 7 machine and beyond. If you have any machine before Windows 7 like Windows XP, Windows Vista then you have to separately install them into your machine. But Microsoft always recommends you upgrade your OS then installing a separate Powershell.

The Powershell version preinstalled with your OS directly depends upon the OS version as shown below (source)

PowerShell Version Preinstalled with Released For
1.0   Windows XP SP2
Windows Server 2003 SP1
Windows Vista
2.0 Windows 7
Windows Server 2008 R2
Windows XP SP3
Windows Server 2003 Service Pack 2
Windows Vista SP1
3.0 Windows 8
Windows Server 2012
Windows 7 SP1
Windows Server 2008 Service Pack 1
Windows Server 2008 R2 Service Pack 1
4.0 Windows 8.1
Windows Server 2012 R2
Windows 7 Service Pack 1
Windows Server 2008 R2 Service Pack 1
Windows Server 2012
5.1 Windows 10 Anniversary Update
Windows Server 2016
Windows 7
Windows Server 2008
Windows Server 2008 R2
Windows Server 2012
Windows Server 2012 R2 (codename: Windows Server 8.1)
Core 6.1   Windows, Mac, and Linux
7   Windows, Mac, and Linux

How to check Powershell Version installed?

Ans: Go to run and type PowerShell and press enter.There type : $PSVersionTable.PSVersion and press Enter. So your Powershell Version is a combination of Major and Minor.

powershell-check-version

As shown here. Powershell the version installed is Major.Minor ie. 5.1

2 Enable execution of Powershell

Only members of the administrative group can change the execution policy of Powershell. Actually, there is 4 execution policy type to execute the script.

a) Restricted: This is the default execution policy for PowerShell. By default, Powershell has Restricted Powershell and no script either local, remote 
or download can be run on the system

b) AllSigned: For this, all the scripts are required to be digitally signed by a trusted publisher.

c) RemoteSigned: All scripts which are downloaded from the internet need to be digitally signed by a trusted
publisher to work. All other local scripts are allowed to run.

d) Unrestricted: If the execution policy is unrestricted then no signature is required to run the script.

You can control the level of trust for executing scripts that run on your system.

 Now, let's check the current execution policy type you have in your system, the command for this is as follow:

Get-ExecutionPolicy

get-executionpolicy.

You can change the current execution policy with the Set-ExecutionPolicy cmdlet to any of the above 4 types.

First of all open Powershell with administrative privileges and type the below command.

Set-ExecutionPolicy

Replace policy name with any of the above 4 policy types.

set-executionpolicy-admin-previledge

Here, I have changed to RemoteSigned Type, now locally all scripts can be executed, and for downloaded scripts required to be digitally signed by a trusted publisher.

Note that this is our time global setting. Then you run it once and never again.

FYI if you have not run with admin privilege you will get the below error.

pwershell-without-admin-previledge

Also Read: How to clear cache windows 10 to boost performance?

If you want to set it back to default then you can set it with Restricted type because it is the default settings provided by Powershell.

Set-ExecutionPolicy Restricted

3 Create the scripts

If you have already created scripts then skip these steps.

Let's create scripts.

For creating a script you just need a text editor like notepad which default comes with Windows.

Open Notepad and write a below simple PowerShell script

Write-Host "Running PowerShell Script"

powershell-text

Then save it with ps1 extension as shown below. Here, I have given HelloWorld as a name.

create-powershell-with-ps1

4 Run the Script

For running the script you can type the below script:

Powershell.exe -File file_location_path

replace file_location_path with the required file path. Here I have helloworld.ps1 inside E:\ so I will type as E:\helloworld.ps1 as shown below.

powershell-output.

as soon as you press Enter you will get the desired output of the script file. This script just displays the message present in the file but you can do lots of automation things with Powershell which is beyond the scope of this tutorial.

Also Read: How to Find or check windows 10 login history?

If your script file is on the current working directory then you just have to type: .\helloworld.ps1 inside of full file path. Here, PowerShell and script file is inside E:\ so I can just type like below.

runpowerscript-from-current-directory

Run Powershell Script without changing policy type

You can run the PowerShell script even without changing the policy type. You can even run your current policy is restricted. For this, you have to use a bypass switch as shown below.

powershell  -executionpolicy bypass -File

If you are having a restricted policy then you are not able to run any scripts (local or remote). Just try it .

powershell-restriction

File E:\helloworld.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies ... 

But there is a safe way, you can load the script without changing any policy with below script

bypass-powershell policy
fig. bypass current policy

How to run a PowerShell script with whitespaces in the path or script name?

We can take the help of the Invocation(&) operator to run the PowerShell script if it has space in the path or in the script name.

Syntax Goes like this:

powershell "& '' "

Suppose I have hello world.ps1 file name inside E:\Powershell Tricks Collection folder then I can execute as shown below.

invocational-operator-powershell

How to run Powershell script from Command Prompt?

We can run PowerShell script from the command prompt with the following command:

powershell <filepath>

example:

powershell "E:/helloworld.ps1"

What is Powershell ISE?

Powershell ISE stands for Powershell Integrated Script Environment. It is the host application for Windows Powershell where we can write, test and debug scripts and has IntelliSense support which helps while writing scripts. It is supported in all versions of Powershell up to Powershell V5.1 and later for Powershell 6 Core, it is removed.

Now you can use ISE mode in VS Code via these extensions.

How to open Powershell ISE ?

Goto run and type "Powershell ISE" then select "Powershell ISE" from the list. Then you will get Powershell ISE as shown below.

powershell-ise

What is Cmdlet?

The cmdlet is a lightweight special PowerShell command which is used to perform single particular functions like rename items, change current directories, get help about particular cmdlet, terminate the process, start PC, etc.It can even be used in command prompt. Some of the cmdlets are

  • Get-Command,
  • Get-Help,
  • Get-Location,
  • Set-Location,
  • Get-ChildItem,
  • Stop-Process etc.