PowerShell 7 is here, and it is spectacular. While it comes with a lot of additions and improvements, the essential thing it accomplishes is once again changing the naming scheme. “Windows PowerShell 5.1”? Lame! “PowerShell 6 Core”? Super dumb! PowerShell 7? Nailed it! Well done, now that we no longer have to worry about extra words. Nothing else is needed for this release to be a complete success, but let’s breakdown a few of the new additions anyway.
Installing PowerShell 7
One thing that will be missing that you are used to with PowerShell 7 is ISE. Microsoft has said for some time that they are done making changes to ISE. You can still use other script editors, and there are some awesome ones out there. I would highly recommend VScode.
What is new
Maintaining Windows compatibility
I will admit I was a reluctant adopter of PowerShell 6 Core, the cross-platform functionality was impressive, but I felt like it lost a little too much of its windows functionality. Because I have always worked in primarily Windows shops, the trade-off was a net loss for me. PowerShell 7 fixes all of that; this is truly the best of both worlds. You no longer need to install a compatibility pack to be able to import some of the modules you used on the day to day. Now, if you have 5.1 installed importing and installing modules will work for you right on install. Commands like Out-Gridview and Get/Set-Clipboard now work as well.
I sometimes watch people throw together quick scripts in front of people with pinpoint precision and get pretty jealous. I seem to run into a minor error after typo after a wrong command. With each misstep I make, I get to dive into the error command. To PowerShell’s credit, everything you need to correct your mistakes is in there, but it is far from readable. Enter Get-Error! This new CMDlet is incredible, All the information you need is still right there, but now it prints out in a format similar to an event log, and I find I can get what I need much quicker now.
To see your latest error is as simple as
You can also pipe $Error into Get-Error and just have that variable printout in the much improved format.
Running your foreach loops parallel
When dealing with a lot of data, you can run into some severe delays in your script. PowerShell 7 will allow you to run your foreach loops parallel now. The amount of time this can save is crazy. A real-world example would be great here, but how about one that does nothing but highlights the value? In this example, we have a list of 30 users. If we run this, it would take at least 30 seconds to complete.
By adding -parallel after ForEach-Object it will run those five at a time more if you adjust the amount with -ThrottleLimit. Be careful with throttle limit, the more you add and the more complex your script, the more you run the risk of overtaxing your machine. With some trial and error, you can cut down your run time for scripts that are working with a large amount of data.
Parallel does not make sense to use in every scenario, but it has a place in your toolbox. I bet if you look into various scripts you have automating your environment right now, you could find several that could be enhanced with this new feature.
Some of the most significant changes that have been made are the ability to cut down the lines you need in your scripts. PowerShell in the past could get rather bulky and cumbersome when writing your code. There are now several options to help you cut down on those extra lines.
I will be honest when I first heard that PowerShell 7 allowed ternary code, I thought it was a neat word. I had no idea what it was, but I mentioned it to one of our developers, and his response was far more enthusiastic. So I figured I better learn what it is.
Ternary Operators can be used to make decisions in place of conditional statements if and else. So instead of these five lines.
You only need this line.
That condenses it quite a bit, but one of the cooler parts of PowerShell is how readable it is. Verb-Noun Pairings for life!!! So is this less readable? I think it may seem so at first, but once you really look into it, I think it might even be easier to read. It breaks down like this
If the start of the line is true, the value is left of the colon; if it is not, it is the right of the colon. That is very readable once you learn that. I will be honest, I have considered digging up old scripts and adding this for the pure joy of making it look better.
Pipeline chain operators
Chain operators work like a replacement for and(&&)/or(||) statements. If you are familiar with writing batch scripts, you have probably already used this. This snippet will run both commands the double ampersand works like an and statement.
This snippet will only run the first command. The double pipe counts as an or statement. If the first command failed in a nonterminating error, it would then run the second.
Chain Operators gain extra utility when you string them together. This command will check for a file. If it exists, it will get the content of the file.
There is a lot of value in there about your script behaving differently based on the results of commands. Using these correctly can make your scripts far more versatile.
There are a lot of great additions in PowerShell 7. I am slow to adapt to changes, and outside of testing 6, I never really used it. PowerShell 7 is truly the best of both worlds. I love it, and it has become my default PowerShell. I think you should for sure give it a shot. If nothing else, take one of your scripts and replace the bulk with some of these new features, I promise it will sell you on it. I should not need to sell this any harder.
Jordan had spent his life wondering why tasks he didn’t like to do had no options to complete themselves. Eventually he had to make that happen on his own. It turned out that he enjoyed making tasks complete themselves, and PDQ thought that is something he should talk about on the internet while drinking most Thursdays on the PDQ webcast.