How To Create and Run PowerShell Scripts? – POFTUT

How To Create and Run PowerShell Scripts?

Powershell scripts are used to complete tasks simply running script files. This provides easy to use ways to run the same commands again and again. Scripts also used to share the experience with other system administrators easily. Scripts may need sometimes parameters for the operator. In this tutorial, we will look at different aspects and usage forms of Powershell scripts.

Enable Powershell Script Execution with Execution Policy

Security is an important topic today. Microsoft has learned a lot of things from previous mistakes. It created some security mechanisms for its powerful scripting engine Powershell.

Print Current Execution Policy

Get-ExecutionPolicy is used to list the current status of the execution policy. We will get like below.

PS> Get-ExecutionPolicy
Print Current Execution Policy
Print Current Execution Policy

As we can see that current execution policy is whichRestricted simply means we can not run downloaded PowerShell scripts.

Enable Execution Policy

We can change the PowerShell execution policy with commandSet-ExecutionPolicy by specifying the new policy name. We will allow all scripts by specifying the policy named Unrestricted . In order to change the current shell should have Administrator privileges. This will run scripts if they are disabled on the system.

PS>  Set-ExecutionPolicy Unrestricted
Enable Execution Policy
Enable Execution Policy

We will simply answer toY the confirmation question.

Create Simple PowerShell Script

In order to use it in the test, we will create a script file named whichhelloworld.ps1 contains the following script. ps1 is used to specify the PowerShell script.

echo "Hello World"

Run Script In The Current Directory

We can simply run the script which resides in the current directory. In this example, we will run helloworld.ps1 . . is the current working directory.

PS> .\helloworld.ps1

Path with Spaces

Spaces have a different meaning in Powershell and path, file or command names with spaces must be surrounded with double  "  or ' single quotes. In this example, we have a Powershell file which is named hello world.ps1. We can see that hello and world are separated with space. In order to call this Powershell script, we need to use " which will surround the name completely.

PS> ".\hello world.ps1"

If we do not use a single or double quote like below we will get a `Not Recognized Name of a cmdlet, function, script file or operable program error” with CommandNotFound type.

PS> .\hello wordl.ps1
Path with Spaces
Path with Spaces

Using Invoke Command Commandlet

Powershell provides Invoke-Command commandlet for command invocation. We can use this for script call. More detailed about Invoke-Command can be found below.

LEARN MORE  Run Commands In Local and Remote System With Powershell Invoke-Command Cmdlet

Run Commands In Local and Remote System With Powershell Invoke-Command Cmdlet

Run With CMD.exe

We can call different shells from Powershell. For example, we can call commandspython and script from Powershell by specifying the python interpreter. We can also runcmd.exe to call MS-DOS scripts like below. But keep in mind that MSDOS can only run bat files.

PS> cmd.exe /C helloworld.bat

Run without Exit

After the script is completed the PowerShell window will be closed as the process is complete. If we need to prevent exit from the command prompt we can use orRuncmd.exe with the following command by providing with option-noexit.

PS> powershell -noexit .\helloword.ps1

Run PowerShell and Script As Administrator

If the script needs Administrator privileges we should start the process with Administrator privileges which will be needed by the script. We can start the Powershell with the by right-clicking existing Powershell screen.

Run PowerShell and Script As Administrator
Run PowerShell and Script As Administrator

Leave a Comment