Script packages and elevation with manifests

Microsoft Windows Vista and Server 2008 have become a lot more protective of certain areas of the registry and the file system. Take an innocent little script that create a URL shortcut in a folder under C:\Program Files. Not really a problem under XP, but under Window Vista this will fail:   Fortunately Windows Vista […]

Read More

Single quotes, not double

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 avoid unintended variable expansion, use […]

Read More

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

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 To add properties to the […]

Read More

Formatting in scripts

Develop a standard for formatting your scripts, and use indentation. Function MyFunction { # function code here } …or… Function MyFunction { # function code here }

Read More

Avoid the use of aliases in scripts

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 your script.

Read More

Don’t modify out-of-scope items

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 on different systems.

Read More

How do I sign a Windows PowerShell script?

First you’ll need a code-signing certificate. If you purchase one, you’ll be looking for a “Class III” digital certificate of the “Microsoft Authenticode” variety. This will often come in two parts: An SPC file, which is the Software Publishing Certificate, and a PVK file, which is the corresponding private key. If you use a utility […]

Read More

Running command-line tools

These examples show how to run command-line tools from within a VBScript, capturing the tool’s output. ‘ ‘ ScriptingAnswers.com Essentials – by Don Jones ‘ ‘ Run cmdline tools ‘ This shows how to launch a command-line ‘ tool and retrieve its output into a string variable ‘ to then do something with it. For […]

Read More

Search

Enter any query and press enter
Advanced search operators:
+ ⟶ Term must appear
- ⟶ Term must not appear
"" ⟶ Exact string must appear
How do I use these?

Join our mailing list

Enter your email address to subscribe to our mailing lists for Monthly Blog Digests, Product Announcements and more (you can choose which you receive once you sign-up!)

Tags

Categories

Archives

Dashboard