Syntax highlighting is one of the best features of a modern IDE or IDE-lite (such as VSCode). Things like IntelliSense, tab completion, matching token highlighting, go-to-definition, and more are all fantastic and make coding faster, easier, and more efficient (not to mention more accurate and consistent); however, a field of white text on a black background (or worse, black text on a white background) just makes everything look the same and hard to follow. Highlighting different tokens with different colors makes them stand out and makes it easier to tell what something is at a glance.
Read MoreError Handling: Basic Primer
It happens: you run a script or execute a command, and something doesn't work quite right. You get an ugly red error. Now what?
There are many ways to handle this. When you're sitting at the console, frequently it comes down to simply reading the error message and figuring out what you forgot or mistyped. These are often the easiest errors to deal with just for the fact that they're interactive by nature, so you get the opportunity to try other things until you get the results you're looking for before proceeding.
Things get a lot more complicated, however, when you're writing a script, especially one that is meant to run without someone sitting at a console to see the errors. At least if the script is being run manually, the person sitting at the console will be able to see that something did go wrong; when it's unattended, though, frequently you wind up losing valuable clues as to what went wrong, so you need to think about how your script could fail and account for that ahead of time with error handling.
Read MoreUseful [String] manipulation techniques with .Methods()
Many times when working with PowerShell, I find myself needing to manipulate text strings. There are a ton of ways you can manipulate strings, and a lot of those are thanks to the built-in methods included on all string objects.
I've covered some of my favorite string manipulation techniques for you here.
Read MoreOne Neat Trick to take objects apart: ConvertTo-JSON
One of the neatest tricks I've come across recently has to do with a need I find I have frequently: to be able to see the full structure of my output. Get-Member and Select-Object * are powerful, but have limits when it comes to discovery.
I go over the PowerShell v3 cmdlet ConvertTo-JSON in this post to look an another way to see what's really in your output.
Read MoreThe (very basic) Anatomy of an Object
Everyone who works with PowerShell beyond a rudimentary level could benefit from a basic understanding of .NET objects.
This post won't show you everything, but it might help peel the curtain back a little to get you started.
Read MoreExecution Policy is not a security countermeasure (Part 2: Group Policy)
In my last post, I wrote about how setting the Execution Policy is not a viable security countermeasure, and is very easy to bypass by malicious actors. The examples provided work when the Execution Policy is set by running "Set-ExecutionPolicy"; however, it does not bypass an Execution Policy set by Group Policy.
This post will demonstrate a way to get around that as well.
Read MoreExecution Policy is not a security countermeasure
While there are some benefits to signing your PowerShell code, security really isn't one of them. If something is run on your system that attempts to execute a malicious PowerShell script, it does not matter what your Execution Policy is set to.
Read on for a brief explanation of what Execution Policy actually affects, and a demonstration of how easy it is to bypass.
Read MorePipe output to the clipboard using clip.exe
Quick one today, a very simple trick I learned a few days ago: if you need to copy and paste your output from the console, instead of using the markup tools built in to cmd.exe, you can simply pass your output directly to the clipboard. No big trick needed for this; just pipe the output to clip.exe and it will be put on the clipboard, ready to be pasted wherever you need. It's built in to the OS, so you don't need to download and install anything.
Let's say I have a list of user account I've grabbed from Active Directory and saved to the variable $users, and I need to send that list in an email. I could enter "$users" by itself on the command line, then select and hit enter to copy. Then I would paste the list...and find I need to strip out all of the white space this creates. It's not hard, but it can be inconvenient sometimes, especially if it's a long list.
Instead, I could just run "$users | clip.exe" and get the same effect, only without jumping through hoops, without the chance of over or under selecting, and without selecting a bunch of extra white space.
It's really that easy. Just add " | clip.exe" to the end and it will throw the screen output into the clipboard directly instead of on to the screen, and then you can paste it normally.
Test execution speed with Measure-Command
When hacking away at a shell or writing a quick on-off script that will only do a handful of things and only once, knowing how fast each part works isn't that big of a deal. When you're writing something that will get reused over and over, especially if it's something that's working against a large collection (such as, for example, running a WMI query against every computer in your domain), knowing how quickly different commands run could be quite important, especially if you're looking to improve execution speed.
To test execution speed, I use Measure-Command.
Read MoreBetter place to PowerShell (say goodbye to cmd.exe)
I'll be honest, I hate cmd.exe, which is what PowerShell uses by default. It's functional, but crude, old fashioned and basic. I had been using ConEmu for some time, and it's an excellent product, but I ran into another that I feel fixes all of the problems with cmd (and even the last nagging issue I had with ConEmu): Cmder. It uses ConEmu for the console emulator, it's completely portable, and when you just need a windows command prompt (rather than PowerShell), it gives you a Bash-like shell experience (unfortunately, trying to get the bash-like enhancements to work in PowerShell broke tab completion for PowerShell cmdlets for me, so I had to stick with PowerShell style tab completion).
Anyway, it's a great functioning, great looking console emulator, and I highly recommend giving it a try.