Exploring experimental features
During the development of PowerShell Core and later with PowerShell 7, the PowerShell team have routinely added new features. Some of these new features could, at least in theory, break existing scripts and are called "experimental." PowerShell does not, by default, enable any of these features. As shown in this recipe, you must enable them explicitly. This approach to experimental features enables you to test these new features and provide the PowerShell team with feedback. Should you find a feature that breaks a script for you, disable it. If you turn on (or turn off) an experimental feature, you need to restart PowerShell 7.
In general, experimental features are not intended to be used in production since the experimental features, by design, can be breaking. Also, experimental features are not officially supported. That being said, so far, these features have been very stable and reliable.
In this recipe, you look at the experimental features available in PowerShell 7.1 as released. If you are using later versions (for example, a PowerShell 7.2 preview release), you may see different experimental features. For a fuller look at PowerShell's experimental features, see https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_experimental_features.
Getting ready
You run this recipe on SRV1
after you install PowerShell 7 and/or Visual Studio Code, and once you have created a console profile file.
How to do it...
- Discovering the experimental features
Get-ExperimentalFeature -Name * |   Format-Table Name, Enabled, Description -Wrap
- Examining the "command not found" result with no experimental features available
Foo
- Enabling one experimental feature as the current user
Get-ExperimentalFeature -Name * |   Select-Object -First 1 |     Enable-ExperimentalFeature -Scope CurrentUser -Verbose
- Enabling one experimental feature for all users
Get-ExperimentalFeature -Name * |   Select-Object -Skip 1 -First 1 |     Enable-ExperimentalFeature -Scope AllUsers -Verbose
- Starting a new PowerShell console
If you are using VS Code to run this recipe, enter Ctrl + Shift + ` to start a new terminal. If you are using the PowerShell 7 console, start a new copy of the console.
- Examining the experimental features
Get-ExperimentalFeature
- Examining output from the "command not found" suggestion feature
Foo
How it works...
In step 1, you use the Get-ExperimentalFeature
cmdlet to discover the available experimental features and their current state, which (by default) looks like this:
Figure 2.49: Discovering experimental features
To test out an experimental feature, in step 2, you run a non-existent command, with output such as this:
Figure 2.50: Examining the "command not found" result
In step 3, you enable the first feature, the PSCommandNotFoundSuggestion
experimental feature for the current user, which looks like this:
Figure 2.51: Enabling an experimental feature for the current user
In step 4, you enable the second experimental feature, PSCultureInvariantReplaceOperator
, which looks like this:
Figure 2.52: Enabling an experimental feature for all users
In step 5, you start a new version of PowerShell. This step produces no output as such.
In step 6, you examine the state of experimental features, noting that two new features are now available, which looks like this:
Figure 2.53: Examining experimental features
In step 7, you re-run the unknown command to observe the "command not found" suggestions, which look like this:
Figure 2.54: Examining the output from the "command not found" suggestion feature
There's more...
In this recipe, you turn on two experimental features and examine one ("command not found" suggestions). In most cases, you should be safe to enable all of the experimental features, but it is always safer to turn them on one by one and test your scripts carefully.