PowerShell Commands for Everyday Usage

Variables

Assign Variable

$var = "string"

Assign Multiple Variables

$a,$b = 0 or $a,$b = 'a','b'

Flip Variables

$a,$b = $b,$a

Strongly Typed Variable

$var = [int]5

Operators

Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators. Additionally, the + and * operators can also be used to work with strings. When you use the * operator, it repeats a string. When you use the + operator, it concatenates strings. 

Assume integer variable A holds 40 and variable B holds 10, then...

Operator

Description

Example

+ (Addition)

Adds values on either side of the operator.

A + B = 50

- (Subtraction)

Subtracts the right-hand operand from the left-hand operand.

A - B = 30

* (Multiplication)

Multiplies values on either side of the operator.

A * B = 400

/ (Division)

Divides the left-hand operand by the right-hand operand.

B / A = 0.25

% (Modulus)

Divides left-hand operand by right-hand operand and returns the remainder.

B % A = 10

Comparison Operators

The following are the assignment operators supported by PowerShell.

Assume integer variable A holds 40 and variable B holds 10, then...

Operator

Description

Example

eq (equals)

Compares two values to be equal or not.

A -eq B = false

ne (not equals)

Compares two values to be not equal.

A -ne B = true

gt (greater than)

Compares the first value to be greater than the second one.

B -gt A = false

ge (greater than or equals to)

Compares the first value to be greater than or equal to the second one.

B -ge A = false

lt (less than)

Compares the first value to be less than the second one.

B -lt A = true

le (less than or equals to)

Compares the first value to be less than or equal to the second one.

B -le A = true

Assignment Operators

The following are the assignment operators supported by PowerShell.

Operator

Description

Example

=

Simple assignment operator. Assigns values from the right side operands to the left side operand.

C=A+B will assign the value of A+B to C

+=

Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand.

C+=A+B is equivalent to C=C+A

-=

Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.

C-=A+B is equivalent to C=C-A

Logical Operators

The following table lists the logical operators.

Assume the boolean variable A is true and variable B is false, then...

Operator

Description

Example

AND (Logical and)

Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

(A -AND B) is false

OR (Logical or)

Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.

(A -OR B) is true

NOT (Logical not)

Called Logical NOT Operator. Use to reverse the logical state of its operand. If a condition is true then the Logical NOT operator will be false.

-NOT(A -AND B) is true

Common Revit Functions

The table below lists sample scripts that when modified may help solve typical issues that users face when implementing PowerShell scripts.

Desired Function

PowerShell Script

Get Element Id

return $eVolveElement.Id

Get Instance Parameter .AsString

${name} = $eVolveElement.LookupParameter("{ParamName}").AsString()

Get Instance Parameter .AsDouble

${name}= $eVolveElement.LookupParameter("{ParamName}").AsDouble()

Get Instance Parameter .AsInteger

${name} = $eVolveElement.LookupParameter("{ParamName}").AsInteger()

Convert to Decimal Inches

${name} = ((($eVolveElement.LookupParameter("ParamName").AsDouble())*12).ToString())

Resolutions to common issues

Issue - I'd like to display a window with information when a script runs.

After adding the script below, a window is displayed when the PowerShell script runs. You can change the displayed text in the window by modifying the value within quotes and change the parameter value to whatever parameter value you desire.

Solution

[System.Windows.Forms.MessageBox]::Show("Value=" + $value)

Example

[System.Windows.Forms.MessageBox]::Show("This is the Level" + $eVolveElement.LookupParameter("Level").AsValueString())

Issue - Always display parameters

Even though I have the Hide Empty Values option enabled, I would like to always display a parameter even if the value is null or empty.

Solution 1

# Check to see if the parameter exists and the value is null or empty
if (-not $eVolveFromParameter)
{
# Assigns a value if null or empty
return "Not Assigned"
}
# If the parameter exists and the value is not null or empty this defines the parameter to use
return $eVolveFromParameter

Solution 2


# Check to see if the parameter exists and the value is null or empty
if (-not $eVolveFromParameter)
{
# If the value is null or empty, "^abort^" is used to hide the parameter in the panel
return "^abort^"
}
# If the parameter exists and the value is not null or empty this defines the parameter to use
return $eVolveFromParameter

Issue - Conditionally show parameters

I would like to conditionally show a parameter. For instance, I only want to display a parameter if the value is greater than 25.

Solution

# Check to see if the parameter value is greater than 25.0
if ($eVolveFromParameter -gt 25.0)
{
# If the value is null or empty, "^abort^" is used to hide the parameter in the panel
return "^abort^"
}
# If the parameter exists and the value is not null or empty this defines the parameter to use
return $eVolveFromParameter

Issue - Cycle through parameters to populate a single field

I would like to check if a parameter exists and if not use another parameter to populate the field. For example, I want to display the Reference Level in my panel, but when I select one element, the parameter name is "Reference Level" and when I select a different element it is "Level".

Solution 1

# Check to see if the parameter exists and the value is null or empty
if (-not $eVolveFromParameter)
{
# If the parameter is nul or empty, the parameter below is used
return $eVolveElement.LookupParameter("Level").AsValueString()
}
else
{
# If the parameter exists and the value is not null or empty this defines the parameter to use
return $eVolveFromParameter
}

Solution 2

# Defines the parameter to compare 
$result = $eVolveFromParameter

# Check to see if the parameter exists and the value is null or empty
if (-not $result)
{
# If the parameter is nul or empty, the parameter below is used
$result = $eVolveElement.LookupParameter("Level").AsValueString()
}
# Check to see if the parameter exists and the value is null or empty
if (-not $result)
{
# If the parameter is nul or empty, the parameter below is used
$result = $eVolveElement.LookupParameter("eV_PackageLevel").AsString()
}
# If the parameter exists and the value is not null or empty this defines the parameter to use
return $result


How did we do?


Powered by HelpDocs (opens in a new tab)

Powered by HelpDocs (opens in a new tab)