Parallel Execution with ForEach in PowerShell

The PowerShell’s way of executing parallel instructions has been somewhat complicated till now using Jobs, Runspaces, etc. With PowerShell 7 and above, the team has greatly simplified this requirement with improvement in ForEach-Object cmdlet. You would need two extra parameters: Parallel and Throttlelimit to execute a set of instructions in parallel.

The Parallel parameter specifies the script block that is run in parallel for each input item. The new ThrottleLimit parameter limits the number of script blocks running in parallel at a given time. The default is 5.

Read More »

Using ternary operator in PowerShell 7

Developers for high level programming language have been familiar with the ternary operator ? : which goes like: <condition> ? <if-true> : <if-false>. It makes for more concise code in case of simple conditions and expressions. With PowerShell 7, this operator has been available and we can utilize the same.

The condition-expression is always evaluated and its result is converted to a Boolean to determine which branch is evaluated next:

  • The <if-true> expression is executed if the <condition> expression is true
  • The <if-false> expression is executed if the <condition> expression is false
Read More »

Change default Login Shell to PowerShell on Linux

With PowerShell 7, PowerShell Core has been improved and resolved the issues with environmental variables in the *-nix based operating systems. If you have been working with Linux Servers and are using PowerShell to perform some kind of administrative tasks, you may also want to set it as default login shell for certain users. This makes it easy to invoke certain scripts since a familiar shell is available when you are calling the environment.

There are many ways to change the shell like using usermod or chsh or may be if you are feeling adventurous, by modifying /etc/passwd directly using vi editor. So let’s get started.

Read More »

Using Pipeline Chain Operators in PowerShell 7

We all know how pipeline is an important and useful feature of PowerShell. It makes it incredibly easy to take output from one expression / cmdlet and pass it to another expression / cmdlet. It has also allowed to chain them together to create complex expressions. However, what was lacking is the ability to conditionally execute the expressions on the right side of the pipeline operator like the more modern versions of Bash shell. PowerShell 7 covers this gap by introducing Pipeline Chain Operators || and &&. These operators allow the conditional execution of commands or expressions depending on whether the previous command / expression succeeded for failed.

Read More »

Using Null Conditional Access Operators in PowerShell 7

Some of the common reasons we check if our variable is null or not, are because we have been trying to access an property on an object which does not exists or misspelled, the object itself does not exist, or iterating through an collection where some members are null or invalid, etc. To deal with these kind of situations, we had to use if..else condition blocks. PowerShell 7 introduced two experimental null conditional access operators: ?. and ?[]. However to enable and use them, you had to use Enable-ExperimentalFeature cmdlet. These have been moved to GA with release of version 7.1.

Read More »

Using Null Conditional Assignment in PowerShell 7

PowerShell 7 introduces few new operators to help sysadmins and dev alike, to work with null values. One of these operator is null conditional assignment operator (??=). Using this operator, allows us to write more cleaner and concise code. In this blog post, we’ll learn about the null conditional assignment operator.

Life Before PowerShell 7

Before PowerShell 7, we needed to use an if condition to check if the variable is null or not using $null operator and then perform the assignment, if that’s the case. For example, consider below code:

Read More »

Using Null Coalescing Operator in PowerShell 7

PowerShell 7 has been around for some time now, with Microsoft and community working on version 7.2. One of the new exciting features introduced in PowerShell 7 is null-coalescing operator i.e. ?? operator. If you have been working on the PowerShell actively, you may have seen situation where you get a null variable from an assignment. More often than not, its because the cmdlet on right side does not returned anything because of whatsoever reasons. Also by default, variables are assigned $null, when not initialized.

Read More »

Enable PowerShell Remoting on Linux using OpenSSH

Linux has always been an important part of the server infrastructure world and SSH has been the primary method of connecting to it. PowerShell can now use same underlying SSH Protocol to perform SSH and manage Linux servers centrally. This functionality allows us to seamlessly incorporate Unix servers using the known PSRemoting commands into the PowerShell scripts. PowerShell based SSH remoting creates a PowerShell host process on the target computer as an SSH subsystem. In this blog post, we’ll learn how we can enable and use the same on one of the popular Linux distributions, Ubuntu. The same set of steps can be used for other Linux distributions as well.

Read More »