Jeffrey Snover explains how scriptblocks can become the value for ANY cmdlet parameter. http://blogs.msdn.com/powershell/archive/2008/04/21/mindwarpingpower-cmdlets-scriptblock-parameters.aspx#comments
Tag: powershell
From the MVP Summit: AD Manageability in PowerShell
If you’ve been following PowerShell, you know that a major pain point has been the pretty lackluster support for AD management. In fact, if it weren’t for Quest’s PowerShell cmdlets, I’d never use the shell for AD management. But no more! We’ve been informed that the AD team is working on a set of cmdlets […]
INCLUDE in PowerShell
I recently had a couple of administrators who were upset that PowerShell doesn’t have a way to "include" files in a script. Basically, the idea is that you make a script which contains a bunch of commonly-used functions, and then "include" those inside other scripts. That way, the other scripts can take full advantage of […]
PowerShell v2 Book: Reviewers Needed
Please read this post in its entirety before responding! As you may know, Jeff Hicks and I have been working on a 3rd edition of our Windows PowerShell: TFM book, which focuses on v2.0 of PowerShell. Of course, PowerShell v2 isn’t released yet, but it is available as a Community Technology Preview (CTP). So we’ve […]
PrimalScript / Alt+X / PowerShell / Mmmm…
Did you know that the latest build of PrimalScript 2007 (Professional and Enterprise) has a cool new feature for Windows PowerShell users? Just highlight some code – an entire line, just a bit, or several lines – and press Alt+X. Only the highlighted code will execute! This opens up a very cool new workflow. Instead […]
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 […]
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 […]
Formatting in scripts
Develop a standard for formatting your scripts, and use indentation. Function MyFunction { # function code here } …or… Function MyFunction { # function code here }
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.
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.