How To Run PowerShell Scripts

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Powershell scripts are used to complete tasks simply running script files. This provides easy to use way 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 to 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 todays. 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.

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 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 disabled on the system.

Enable Execution Policy
Enable Execution Policy

We will simply answer toY the confirmation question.

Create Simple Script

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

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.

LEARN MORE  Linux Bash Creating and Running Script File

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.

If we do not use 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.

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.

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

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 command prompt we can use orRuncmd.exe with the following command by providing with option-noexit.

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.

LEARN MORE  How To Add New User Account To Linux
Run PowerShell and Script As Administrator
Run PowerShell and Script As Administrator

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM