Creating Helper Functions in PowerShell

Whenever you’re working in PowerShell running commands to do these things, you probably don’t think about how those commands actually work. All of the default cmdlets have a single “interface” which is the command name. To get the contents of a file, you’ll run Get-Content. To ping a computer, you’d just run Test-Connection and so on. However, when you start creating your functions and modules, you’ll soon realize that these commands are doing a lot of work under the covers for you. Lots of commands in PowerShell need a little help to work efficiently. There are times when commmands don’t just do one thing. For example, Get-Content doesn’t just “get content”. Instead, it has to find the file on the disk, open the file handle, iterate over each line of the file, close the file handle and then return the output to you. Get-Content is performing lots of actions all […]

Read more

Learn to Build Tools not Code

Have you ever heard the term “code abstraction”?  The word means creating an interface of sorts around another piece of code that compartmentalizes that code. Think of it as an action or task that bundles a bunch of code up into a package which could then be used later. The concept is like a function. In PowerShell, we have the Get-Content command. When you run the Get-Content, it doesn’t just inherently know how to read a file. There’s code inside of Get-Content that finds the file on the disk, reads the file, open an object, close an object, runs a method, and so on.  But for you and I, we can just run Get-Content by providing a file path, and it just works. That’s one of the great things about PowerShell. PowerShell abstracts away a lot of the things that you don’t need. That same concept can be thought of […]

Read more

Coding Close to the Source

As you’re building more and more automation scripts you’re going to inevitably come across instances where you have to make a choice. Do I take the easy road or do I buckle down and learn something I’m not familiar with to ultimately create better code? I saw a tweet recently that said “The difference between a junior and a senior developer is knowing which yaks to shave. This rings true in the scenario I’m about to share with you. I’ve recently been working on a project that has puts me at odds with Microsoft’s Team Foundation Server (TFS). Previous to the current job I’m on, I had never touched TFS before. It was a foreign concept and, frankly, still is, for the most part. To me, it’s an unwieldy beast that kicks my ass on a daily basis but, I digress. Knowing my experience with this product is important so […]

Read more

Powershell’s Comparison Operators: What’s -eq, -ne, -ceq and -cne?

Coming from a software development background the first instance I had a “Huh?” moment was when I first saw Powershell’s comparison operators eq, ceq, cne, etc.  All other languages have comparison operators but not quite like Powershell.  I was used to == and <>.  Not only does the syntax look completely different than your typical language they also behave differently when dealing comparing collections of values.  It’s something that really hung me up when learning about Powershell but now it seems like ol’ beer adage; it’s an acquired taste.  Let’s go over briefly what a comparison operator is if you’re unfamiliar and dive deeper into the nuances that Powershell introduces. As I mentioned, all programming/scripting languages have comparison operators.  In their most basic sense, comparison operators are necessary to evaluate how different things compare to other things.  They are used to take two objects and see what the difference (if […]

Read more

What Makes a Good Script? It Works!

When you’ve got two geeks discussing Powershell around the water cooler you’ll inevitably get a disagreement on how best to solve a problem.  “No!  An if/then statement would be much better to test that condition rather than switch statement” or “I’d write a function there instead of using that same code again.  That’s so inefficient!”. Writing code in whatever language you choose is going to always be open for interpretation.  This is why I’ve seen the argument that coding can actually be considered art.  Just like with a great Picasso piece, some people may love it and some people may hate it.  It’s open for interpretation.  However, what makes code different than a painting is that it has function behind it.  The code must perform some action after it’s completed rather than just look pretty. In the book The Pragmatic Programmer (affiliate link), the author explains that not only does your […]

Read more

OutputType (): Optional but Recommended

When writing advanced functions in Powershell you have ton of optional things you can add to make your scripts more usable. One of those is using OutputType() . Admittedly, I’ve never used  OutputType()  in my scripts. I have no real excuse. I just blame my ignorance. At first I thought, why would I need to do this? The scripts work fine without it. That is until I came across Lee Holmes’ excellent blog post entitled What is OutputType()?. Not only does using  OutputType()  add tab-completion functionality to your object but the Powershell ISE also leverages this for Intellisense. Pretty cool! Rather than rambling on about this obscure topic I’ll just leave you with another link to Bartek Bielawski’s blog post entitled OutputType – Why Would You Care?. Bartek does a great job in explaining in a little more in-depth as to why you’d want to use OutputType() .

Read more

Quit With the Text Files Already

As a Powershell beginner you’re probably a Googling maniac; I was definitely.  As you go out and find script snippets out there you’ll find that a ton of examples demonstrate a concept of reading computer names from a text file.  When someone is demonstrating how to run a command on multiple computers it is the easiest way; just dump some computers in a text file, save it and then use Get-Content or Import-CSV.  I strongly encourage you to NOT do this. Using a text file isn’t necessarily bad practice to do so.  It’s because you’re probably performing some manual process to get those computers into the file when you don’t have to!  Instead of dropping in a bunch of computer names into a file I strongly encourage you to think about how you’re getting those computer names and set that criteria in the script. When you just blindly dump a bunch […]

Read more

The Hard Part is not Powershell

I’ve been writing scripts in one shape or form for over 20 years now. During this time I’ve written scripts in batch, AutoIT, AutoHotkey, SQL, PHP, ASP, vbscript and Powershell. I love automation and typically take hold of whatever scripting language I need to use. If anything, writing literally thousands of scripts to do all kinds of crap has taught me that the hard part is not getting the script to do what you want it do; it’s figuring out what you want to do in the first place! When faced with learning a new programming/scripting language I found the core methodology to be all very similar.  Every modern OOP language has variables, constructs, objects, properties, methods, etc.  You can always loop over a container of items whether you call the container a collection, an array, a hash table, an associative array and array list…whatever.  You get the point.  At their root, […]

Read more

A Little Code Change Makes a HUGE Difference

I was tinkering around with a script the other day and came across a requirement I have in nearly every script. The requirement was to iterate through a collection of objects, do something to each of the object’s properties in the fastest, most efficient way possible and eventually get a collection of strings as output. I started out just throwing in my usual method of doing this via Select-Object’s calculated properties and I got to thinking if there’s a better way. Usually, I have less than a few thousand objects to iterate through but what if I had millions? Using a calculated field surely can’t be the best way, can it? I set out to find the truth. I decided to post up a Powershell.org forum thread entitled Is there a better way to expand a calculated field? that raised the question to others and I got a few interesting […]

Read more