PDQ.com mobilePDQ.com desktop
Support

What Is New In PowerShell 7.0

Jordan HammondJordan Hammond
·

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

Installing is very quick and intuitive. You can go to GitHub and download the installer and install it quickly. Alternatively, you could zazz it up with some PowerShell.

Invoke-Expression "& { $(Invoke-RestMethod -Uri https://aka.ms/install-powershell.ps1) } -UseMSI"

Running that will execute a PowerShell script to download and install the latest release of PowerShell 7. You are now at the highest tier of awesome by using PowerShell to Install PowerShell.

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. There is some setup required to get it working optimally, but Seline breaks down how to set all that up here.

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.

Get-Error

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

Get-Error -Newest 1

You can also pipe $Error into Get-Error and just have that variable printout in the much improved format.

$Error | Get-Error

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.

$user | ForEach-Object {
Start-Sleep -seconds 1
$_.Username
}

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.

Streamlining Code

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.

Ternary Operators

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.

if($food -eq "Delicious") {
$value1 = "Sammich"
} else {
$value1 = "Tofu"
}

You only need this line.

(($food -eq "Delicious") ? "Sammich" : "Tofu")

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

Condition ? Value if true : Value if false

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.

Write-Output "first" && Write-Output "second"

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.

Write-Output "first" || Write-Output "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.

Get-ChildItem "C:\temp\important.txt" || Write-Error "No File!!!!" && Get-Content "C:\temp\important.txt"

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.

Conclusion

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. The Power of Shell compels you to automate your environment!!

Don't miss the next post!

August 2020 Patch Tuesday Vulnerabilities

August 2020 Patch Tuesday Updates and Vulnerabilities

PDQ.com
© 2020 PDQ.com Corporation

Products

  • Deploy
  • Inventory
  • Enterprise SL
  • Pricing
  • Downloads
  • Licensing
  • Buy