Great Debate: The Conclusion

All this Summer, we’ve been encouraging your feedback in a series of Great Debate posts. Most of the topics came from the 2013 Scripting Games, where we definitely saw people coming down on both sides of these topics. My goal was to pull everyone’s thoughts together into a kind of community consensus, and to offer a living book of community-accepted practices for PowerShell. This’ll be a neverending story, likely adapting and growing to include more topics as the years wind on. But here’s the start: DRAFT-2013Sep_Practices is the first draft, officially a Request For Comments, based on the comments you’ve all contributed […]


Great Debate: Pipeline Coding Syles

A good programmer or scripter should always try to produce code that is easy to read and understand. Choosing and consistently applying a strategy for indentation and bracing will make your life, and possibly those of your co-workers, much easier. (Unless, of course, you’re that guy who prides himself in writing code that looks like a cat walked across his keyboard, and cackles any time someone tries to understand it.) Much of PowerShell’s syntax can borrow coding conventions from C-style languages, but the pipeline is something new. Below are two pipelines formatted a few different ways. Which styles do you […]


PowerShell Great Debate: “Fixing” Output

When should a script (or more likely, function) output raw data, and when should it “massage” its output? The classic example is something like disk space. You’re querying WMI, and it’s giving you disk space in bytes. Nobody cares about bytes. Should your function output bytes anyway, or output megabytes or gigabytes? If you output raw data, how would you expect a user to get a more-useful version? Would you expect someone running your command to use Select-Object on their own to do the math, or would you perhaps provide a default formatting view (a la what Get-Process does) that […]


PowerShell Great Debate: What’s Write-Verbose For?

This was a fascinating thing to see throughout The Scripting Games this year: When exactly should you use Write-Verbose, and why? The same question applies to Write-Debug. “I use Write-Debug to provide developer-level comments in my scripts, since I can turn it on with -Debug to see variable contents.” “I use Write-Verbose to provide developer-level comments in my scripts, since I can turn it on with -Debug to see variable contents.” See what I mean? Some folks will suggest that Verbose is for “user-friendly status messages;” others eschew Debug entirely and prefer PSBreakpoints for that functionality. What guidance would you offer for using Write-Verbose […]


PowerShell Great Debate: Can You Have Too Much Help?

In The Scripting Games this year, more than a few folks took the time to write detailed comment-based help. Awesome. No debating it – comment-based help is a good thing.  But some folks felt that others took it too far. There were definitely scripts where the authors used, for example, the .NOTES section to explain their thinking and approach. Some commenters felt it was excessive, while others have pointed out, “wow, what if every programmer gave us some idea what the heck he/she was thinking at the time?” Some felt these extensive comments were just at attempt to get a better […]


PowerShell Great Debate: Script or Function?

One of the most frequent comments in The Scripting Games this year was along the lines of, “you should have submitted this as a function, not a script.” Of course, the second-most frequent comment was something like, “you shouldn’t have submitted this as a function.” Let’s be clear: if an assignment explicitly asks for a function, you should write one. What we’re debating are the pros and cons of a single tool being written one way or another. Read that again: a single tool. If you’re writing a library of tools, it’s obvious that writing them as functions for inclusion in a single […]


PowerShell Great Debate: PowerShell Versions?

Today’s Great Debate is a bonus, offered from former team member June Blender. Take it away, June! Like several of the excellent debates in our Great Debate series, this debate issue arose during in Scripting Games 2013 when different judges used different selection criteria to evaluate entries. Some judges, like me, wanted to see evidence that the scripter had studied all features of the newest version of the Windows PowerShell language and selected the best approach for their solution. Other judges wanted the solutions to work on as many computers as possible. Outside of the Scripting Games, this issue is […]


PowerShell Great Debate: The Purity Laws

This should be interesting. During The Scripting Games, I observed (and in some cases made) a great many comments that I’m lumping under the name “Purity Laws.” You shouldn’t use a command-line utility like Robocopy in a PowerShell script. You shouldn’t use .NET classes in a PowerShell script. You should map a drive using New-PSDrive, not net use. And so on. You see where I’m going: there are folks out there who feel as if the only thing that goes into a PowerShell script is Pure PowerShell. Which is odd, because it isn’t an approach the product team actually gave much […]


PowerShell Great Debate: Credentials

Credentials suck. You obviously don’t want to hardcode domain credentials into a script – and PowerShell actually makes it a bit difficult to do so, for good reason. On the other hand, you sometimes need a script to do something using alternate credentials, and you don’t necessarily want the runner of the script to know those credentials. So how do you deal with it? Let’s be clear: This is not a wish list. Comments like, “I wish PowerShell could do ____” aren’t valid. What do you do using the technology as it exists today? Do you prompt for a credential and assume the script […]


PowerShell Great Debate: Piping in a Script

Take a look at this: # version 1 Get-Content computers.txt | ForEach-Object { $os = Get-WmiObject Win32_OperatingSystem -comp $_ $bios = Get-WmiObject Win32_BIOS -comp $_ $props = @{computername=$_; osversion=$os.version; biosserial=$bios.serialnumber} New-Object PSObject -Prop $props } # version 2 $computers = Get-Content computers.txt foreach ($computer in $computers) { $os = Get-WmiObject Win32_OperatingSystem -comp $computer $bios = Get-WmiObject Win32_BIOS -comp $computer $props = @{computername=$computer; osversion=$os.version; biosserial=$bios.serialnumber} New-Object PSObject -Prop $props } These two snippets do the same thing. The first uses a more “pipeline” style approach, and I’ve personally never felt the urge to do that in a script. Probably habit – […]


PowerShell Great Debate: Backticks

Here’s an age-old debate that we can finally, perhaps, put an end to: The backtick character for line continuation. The basic concept looks like this: Get-WmiObject -Class Win32_BIOS ` -ComputerName whatever ` -Filter "something='else'" This trick relies on the fact that the backtick (grave accent) is PowerShell’s escape character. In this case, it’s escaping the carriage return, turning it from a logical end-of-line marker into a literal carriage return. It makes commands with a lot of parameters easier to read, since you can line up the parameters as I’ve done. My personal beefs with this: The character is visually hard […]


PowerShell Great Debate: Formatting Constructs

Here’s an easy, low-stakes debate: How do you like to format your scripting constructs? And, more importantly, why do you like your method? For example, I tend to do this: If ($this -eq $that) { # do this } else { # do this } I do so out of long habit with C-like syntax, and because when I’m teaching this helps me keep more information on the screen. However, some folks prefer this: if ($this -eq $that) { # do this } else { # do this } Because of my own long habits, I find that hard to read, […]


PowerShell Great Debate: To Accelerate, or Not?

At his Birds of a feather session at TechEd 2013, Glenn Sizemore and I briefly debated something that I’d like to make the topic of today’s Great Debate. It has to do with how you create new, custom objects. For example, one approach – which I used to favor, but now think is too long-form: $obj = New-Object -Type PSObject $obj | Add-Member NoteProperty Foo $bar $obj | Add-Member NoteProperty This $that We saw some variants in The Scripting Games, including this one: $obj = New-Object PSObject Add-Member -InputObject $obj -Name Foo -MemberType NoteProperty -Value $bar I generally don’t like […]


PowerShell Great Debate: Capturing Errors

Hot on the heels of our last Great Debate, let’s take the discussion to the next logical step and talk about how you like to capture errors when they occur. The first technique is to use -ErrorVariable: Try { Get-WmiObject Win32_BIOS -comp nothing -ea stop -ev mine } Catch { # use $mine for error } Another is to use the $Error collection: Try { Get-WmiObject Win32_BIOS -comp badname -ea stop } Catch { # use $error[0] } And a third is to use $_: Try { Get-WmiObject Win32_BIOS -comp snoopy -ea stop } Catch { # use $_ } […]


PowerShell Great Debate: Error Trapping

In the aftermath of The Scripting Games, it’s clear we need to have several community discussions – thus, I present to you, The Great Debates. These will be a series of posts wherein I’ll outline the basic situation, and you’re encouraged to debate and discuss in the comments section. The general gist is that, during the Games, we saw different people voting “up” and “down” for the exact same techniques. So… which one is right? Neither! But all approaches have pros and cons… so that’s what we’ll discuss and debate. In the end, I’ll take the discussion into a community-owned […]