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()

Get Type Parameter .AsString

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

Convert to Decimal Inches

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

Rounding to nearest fraction

${name} = ([Math]::Round($value) * X) / X))

$value = decimal feet

X =delimater *12

1/8" = 96

1/4" = 48

1/2" = 24

1" = 12

Concatenate parameters and additional text

${name1} = $eVolveElement.LookupParameter("{ParamName1}").AsDouble()

${name2}= $eVolveElement.LookupParameter("{ParamName2}").AsString()

return "${name1} ${name2} additional text"

Split ValueString and return single word from specific position

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

return ${name} = (${name}.Split(" "))[X]

where X is the location of the desired word in the string, starting with 0

Example ValueString to split: EMT Compression Coupling

Example Script:

$Example = $eVolveElement.LookupParameter("ExampleParam").AsValueString()

return $Example = ($Example.Split(" "))[1]

Returns: Compression

Get Location

return $eVolveElement.Location.Point

Returns: ({x}, {y}, {z})

return $eVolveElement.Location.Point.X

Returns: {X}

return $eVolveElement.Location.Point.Y

Returns: {Y}

return $eVolveElement.Location.Point.Z

Returns: {Z}

Use a data table to lookup a value and return a separate value

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

${name2} = $eVolveData_{TableName}("[{ColumnToCompare}]=${name1}")[0]["ColumnToReturn"]

return ${name2}

Check if a string value returned from a data table is null or empty

if([String]::IsNullOrWhiteSpace($name))

{return "Empty"}

else

{return "Not Empty"}

Resolutions to common issues

Issue - I'd like to change the formatting of the displayed values.

Values in the Revit database are stored in imperial decimal feet; Revit then converts the stored values to the format set in Project Units. From the eVolve Data Profiles feature, when the Use Display Value checkbox is selected, the stored imperial value is converted to the elected unit set for the project. The issue arises when running a PowerShell script and the return value differs from the base parameter's type, i.e., if the base parameter type is text but the desired formatting for the return value is length, the formatting will only reflect that of a text parameter. The solution is to select a base parameter of the same format type you would like the return value displayed.

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

Issue - Round Numbers

The following solution allows the value to retain its number attribute without having to convert it to a string. Solution 1, return a whole number. Solution 2, returns a decimal value by simply adding a comma and specifying the number of decimal places to return.

Solution 1

${name}= $eVolveElement.LookupParameter("{ParamName}").AsDouble()
return [math]::Round(${name})

If the original value were 12.87567894231564, the returned value would be 12

Solution 2

${name}= $eVolveElement.LookupParameter("{ParamName}").AsDouble()
return [math]::Round(${name},3)

If the original value were 12.87567894231564, the returned value would be 12.875


How did we do?


Powered by HelpDocs (opens in a new tab)