CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Scripts and Defining Parameters

So far we have discussed about PowerShell cmdlets; and we have discussed these commands with working examples. The commands were executed at the PowerShell command window. Does it possible to add these commands to a file and execute them from the prompt? Yes, this is where Scripts comes into the picture.

PowerShell Scripts

Scripts are the files contains one or more PowerShell commands (cmdlets). The Script file name has the “.ps1” extension. For example, text.ps1 is the PowerShell script file contains PowerShell command(s).

When you want to run multiple PowerShell commands; add them into a file and save it as a PowerShell script file. This will allows you to share the file and also makes easy to run the commands as a single PowerShell command. It allows to use the commands whenever required and also it is easy to manage the Scripts.

When you perform any administrative activities; all the related commands will be stored in a Script file and run the file whenever required.

Running a PowerShell Script

We can run PowerShell scripts from the Windows Explorer or from the PowerShell command prompt.

To run it from the Windows Explorer; select the PowerShell script and right click on it. Windows Explorer displays a context menu; then select “Run with PowerShell” menu option to run the Script. Note that this is available from PowerShell 3.0 only. When you run it; it starts a PowerShell session that has Bypass execution policy and once the script runs; closes the session.

It sets the execution policy with below command:

-Command
if((Get-ExecutionPolicy ) -ne AllSigned) { Set-ExecutionPolicy -Scope Process Bypass }

This feature is useful when you have the scripts without arguments to be passed.

Another way of running the PowerShell script is through the PowerShell console window by typing the complete Script file path or by typing the relative path of the file.

PS C:\> C:\PowerShell\test.ps1
Hello! This is displayed through the Script

To display the content of the “test.ps1” file; here is the command:

PS C:\> cat .\PowerShell\test.ps1
Write-Output "Hello! This is displayed through the Script"

Defining Parameters to PowerShell Script

We can define arguments to PowerShell Scripts and these are defined using param statement. The param statement should be the first one in PowerShell script. Ones parameters are defined, we can pass argument values when running the PowerShell scripts by entering parameter name prefixed with an hyphen “-” and it’s value. For example, to pass a value to the Name parameter enter the value like -Name "Code Steps".

Here is the example of the Script with a parameter defined.

param ($Name)
Write-Output "Hello, $Name! This is displayed through the Script"

And we can pass the arguments like below:

PS C:\> C:\PowerShell\test.ps1 -Name "Code Steps"
Hello, Code Steps! This is displayed through the Script

We will discuss more topics as we go.

[..] David

PowerShell – Scripts and Defining Parameters

One thought on “PowerShell – Scripts and Defining Parameters

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top