Browse CLASSROOM TRAINING REGISTER for classroom training CONTACT us

SAPIEN Solutions

SAPIEN homepage
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

Archive for the ‘Best Practices’ Category

Single quotes, not double

Sunday, March 23rd, 2008 by Don Jones
Use 'single quotation marks' around string literals unless you explicitly need variable expansion. In PowerShell, single quotes are not expanded into variables: $var = 'Hello' $result = '$var $var' # $result contains '$var $var' Double quotes are: $var = 'Hello' $result = "$var $var" # $result contains 'Hello Hello' To…   More »

Emit custom objects, rather than text, from functions and scripts

Sunday, March 23rd, 2008 by Don Jones
Whenever possible, functions (especially) and scripts (to a lesser degree) should emit custom objects, not just text. This ensures that the output of these constructs can be piped to other cmdlets, like Where-Object, Sort-Object, and so forth, further extending PowerShell's capabilities. To create a custom object: $obj = New-Object PSObject…   More »

Formatting in scripts

Sunday, March 23rd, 2008 by Don Jones
Develop a standard for formatting your scripts, and use indentation. Function MyFunction { # function code here } ...or... Function MyFunction { # function code here }…   More »

Avoid the use of aliases in scripts

Sunday, March 23rd, 2008 by Don Jones
Aliases are harder to read and maintain over the long term - so try and avoid using them in a script. If you use PrimalScript, just select Edit > Convert > Alias to Cmdlet to turn all aliases into their full cmdlet names, providing easier long-term maintenance and readability for…   More »

Don’t modify out-of-scope items

Sunday, March 23rd, 2008 by Don Jones
Scripts, functions, and other elements should NEVER modify the variables, aliases, PSDrives, or other items from parent scopes. Instead, return data to the parent scope using the pipeline. Changing parent scope items creates code which is much more difficult to debug and maintain, and can create inconsistent results when run…   More »


Entries (RSS) and Comments (RSS).