IT tutorials
 
Applications Server
 

Sharepoint 2010 : Windows PowerShell Scripts (part 2) - Executing Scripts, Using Parameters in Scripts

11/18/2014 8:17:26 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Executing Scripts

Let’s start by creating a simple script and executing it. The following code is placed in a file named myScript.ps1.

# One-line comments in scripts are written after a number sign.
$args
<#
Block Comments can be written over
multiple lines.
#>

The script starts with a comment, which is preceded by a # sign to indicate it is a comment. Next, it uses the automatic variable $args to display the arguments passed to the script. It ends with a block comment, which is written over multiple lines and enclosed with <# and #> characters.

Run the script as follows:

PS > .\myScript.ps1 "hey" "hey" "my" "my"
hey
hey
my
my

Windows PowerShell does not execute scripts in the current directory by default. You need to explicitly tell Windows PowerShell that the script is placed in the current directory by typing .\ before the script name. Alternatively, you can type its full path:

PS > C:\Scripts\myScript.ps1 "hey" "hey"
hey
hey

If the directory name contains whitespace, you can use the call operator (&) to execute the script. Here’s an example:

PS > & 'C:\My Scripts\myScript.ps1' "hey"
hey

Using Parameters in Scripts

The param statement is used to add parameters to scripts. The param statement must be the first executed line of code in a script (except for comments or comment-based help). Script parameters work in the same way as function parameters.

The following shows a basic script using parameters.

param([string]$firstname, [string]$lastname)
"FirstName: $firstname"
"LastName: $lastName"

We can execute the script by typing .\, the script name (myScript.psi in this example), followed by the parameters.

PS > .\myScript.ps1 -firstname Niklas -lastname Goude
FirstName: Niklas
LastName: Goude
 
Others
 
- Sharepoint 2010 : Windows PowerShell Scripts (part 1) - Setting the Execution Policy
- Sharepoint 2010 : Windows PowerShell Functions
- Sharepoint 2013 : Security and Policy - SharePoint Users
- Sharepoint 2013 : Security and Policy - Permissions and Permission Levels (part 2) - Creating Custom Permission Levels
- Sharepoint 2013 : Security and Policy - Permissions and Permission Levels (part 1)
- Sharepoint 2013 : Security and Policy - Security Administration
- Installing Exchange Server 2010 : Command-Line Setup (part 2) - Command-Line Server Recovery Options , Command-Line Delegated Server Installation , Installing Language Packs
- Installing Exchange Server 2010 : Command-Line Setup (part 1) - Command-Line Installation Options
- Installing Exchange Server 2010 : Graphical User Interface Setup
- Installing Exchange Server 2010 : Preparing for Exchange 2010 Ahead of Time (part 2) - Preparing the Active Directory Forest,Preparing Additional Domains
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us