Everything is case insensitive, comments with # symbol
Conditions
if (condition)
{
#code
} else if (condition)
{
#code
} else
{
#code
}
if (condition1 -And condition2 -And !(condition3))
{}
Negations must be in extra brackets ex:
$x = $true
if ($x) {}
if (!($x)) {}
Parameters
There is special block of code in param, that must be first executable code in function or script. Can be after comments.
Example:
#This is a beginning of script file
param
(
[string] $PackageName = “blah”,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $PackageUrl,
[parameter(Mandatory=$true)]
[string] $VariableName
)
You can call the script or function, either with parameters in order in which they are defined like
blah a b c
or with switch
blah -PackageName “a” -PackageUrl “b” -VariableName “c”
Types
[string] $x = “blah”
[bool] $x = $false #or $true
#store output of a command into string
$rev_list = git rev-list HEAD –count | Out-String
#replace new line
$rev_list = $rev_list.Replace(“`n”, “”).Replace(“`r”, “”)
Functions
# Example function with example call of that function
$ErrorActionPreference = “Stop”
function PackageTest
{
param
(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $PackageName,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $PackageUrl,
[parameter(Mandatory=$true)]
[string] $VariableName
)
Write-Host “Looking for $PackageName… ” -NoNewLine
if (!(Test-Path $PackageUrl))
{
echo “ERROR”
echo “Unable to find $PackageName at $PackageUrl, you can set the alternative path using -$VariableName=path”
exit 1
}
echo (“OK”);
}
PackageTest “Qt5” “$qt5_path” “qt5_path”