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.
As we can see that current execution policy is which
Restricted simply means we can not run downloaded PowerShell scripts.
Enable Execution Policy
We can change the PowerShell execution policy with command
Set-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
We will simply answer to
Y the confirmation question.
Create Simple PowerShell Script
In order to use it in the test, we will create a script file named which
helloworld.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
. is the current working directory.
Path with Spaces
Spaces have a different meaning in Powershell and path, file or command names with spaces must be surrounded with double
' 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
Using Invoke Command Commandlet
Invoke-Command commandlet for command invocation. We can use this for script call. More detailed about
Invoke-Command can be found below.
Run With CMD.exe
We can call different shells from Powershell. For example, we can call commands
python and script from Powershell by specifying the python interpreter. We can also run
cmd.exe to call MS-DOS scripts like below. But keep in mind that MSDOS can only run
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 or
Runcmd.exe with the following command by providing with option
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.