Ultimate Guide to create and run PowerShell Scripts?


👤 Diwas Poudel    🕒 29 May 2022    📁 TECH

PowerShell is Microsoft developed a powerful tool for task automation and configuration management consisting of an 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 was later available for other OS as well. Initially, it was built on the .Net Framework which includes Powershell 1.0, 2.0,3.0, 4.0,5.0, and 5.1. Later it was built on the .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 a 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, and image files you cannot run PowerShell script files by double-clicking it. To run Powershell scripts you need to follow the following steps.

Steps for running PowerShell scripts

  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 on your Windows 7 machine and beyond. If you have any machine before Windows 7 like Windows XP or Windows Vista then you have to separately install them into your machine. But Microsoft always recommends you upgrade your OS and then install 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 They are made for Windows XP SP2, Windows Server 2003 SP1, and Windows Vista and have to be separately installed. 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 do check Powershell Version is 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 the 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 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 using Notepad

3 a Create Powershell Script using Notepad

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

3 b Create Powershell Script using Integrated Scripting Environment

You can use inbuilt Powershell ISE for creating the PowerShell scripts and the steps are as follows:

1 Press the Windows + R keyboard shortcut to open the run dialog box. There type powershell_ise.exe and press Enter. This will open Powershell ISE.

2 Click on File > New to create a new PowerShell script. Then type your script there.

create-new-powershell-using-Powershell-ISE
fig. Create a new PowerShell script using ISE
write powershell script
fig. Write PowerShell script here.

3 Then press Ctrl + S keyboard shortcut to save the file and give it a name to the script. In my case I have named it for example: helloworld.ps1 and then click on the Save button.

give name to powershell
fig. give a name to Powershell script

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 the 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 the PowerShell script from the command prompt with the following command:

powershell

example:

powershell "E:/helloworld.ps1"

If you have not enabled Powershell script execution on your System you have to add the parameter as : -ExecutionPolicy Bypass

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

How to install Powershell in Ubuntu Environment?

Ans: Windows Environment comes preinstalled with Powershell and if you want to install Powershell in Ubuntu Environment then follow below steps:

Here, I am showing installing and using Powershell 6 in Ubuntu 16.4.

# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -


# Register the Microsoft Ubuntu repository
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee
/etc/apt/sources.list.d/microsoft.list


# Update apt-get
sudo apt-get update


# Install PowerShell
sudo apt-get install -y powershell


# Start PowerShell
powershell

After registering the Microsoft repository the first time as a superuser, all that is required to keep it up to date after that is to use the command sudo apt-get upgrade Powershell. Then you need to simply run Powershell.