PowerShell的下一个主要版本已经发布,它带来了一些重大变化。第七版包括并行执行、远程执行导入模块、新运算符等功能。在这篇文章中,我们将研究PowerShell 7.0上的所有(PowerShell 7.0)新(New)功能。
PowerShell 目录更改
当您安装 PowerShell 7(install PowerShell 7)时,它将安装到一个新目录,并与PowerShell 5.1一起运行。如果您是从PowerShell Core 6.x升级,那么它将覆盖PowerShell 6目录,并删除所有不必要的文件。这是您应该知道的目录列表:
PowerShell 7 安装到 -
%programfiles%\PowerShell\7
% programfiles %programfiles%\PowerShell\7文件夹被添加到 $env:PATH
PowerShell 7安装程序包升级(PowerShell 7)PowerShell Core 6.x的早期版本:
Windows 上的 PowerShell Core 6.x:(PowerShell Core 6.x on Windows:)
%programfiles%\PowerShell\6
被替换为
%programfiles%\PowerShell\7
Linux:
/opt/microsoft/powershell/6
被替换为
/opt/microsoft/powershell/7
macOS:
/usr/local/microsoft/powershell/6
被替换为
/usr/local/microsoft/powershell/7
PowerShell 7 中的新功能
我已尝试简要解释这些功能中的每一个以及它将如何帮助PowerShell 用户(users)。但是,请务必在Microsoft官方页面上阅读更多详细信息。
- 管道并行化
- 新运营商
- ConciseView 和Get-Error cmdlet
- 自动新版本通知
- (Invoke DSC)直接从PowerShell 7调用 DSC资源
- 兼容层
在测试实际代码之前,请务必检查环境变量。(Environment Variable)
1]管道并行化
您现在可以在使用 ForEach-Object -Parallel 时并行执行或处理对象,而不是使用序列方法。在我们关于卸载WIM的帖子中,此方法可以在不到 10 秒的时间内卸载三个WIM映像,而不是按顺序运行时几乎需要 25 秒。(WIM)这是它的示例代码:
Get-WindowsImage -Mounted | foreach -Parallel {Measure-Command {Dismount-WindowsImage -Discard -Path $_.Path}}
2] 新运营商
新增了三个运算符Ternary operator: a ? b : c, Pipeline chain operators: || and && and Null conditional operators: ?? and ??=.这些行为类似于简化的 if-else 语句。(behaves like a simplified if-else statement.)它们使编写代码变得容易,而不是一直使用 If-else 循环。
3] ConciseView和 Get-Error cmdlet
ConciseView是一个用户可选择的视图,它作为(ConciseView)PowerShell 7的默认视图启用。如果错误不是来自脚本,您将收到单行错误。但是,如果它来自脚本或存在解析错误,您将收到多行错误消息,以及显示错误发生在哪一行的指针。
然后,您将拥有一个新的 cmdlet Get-Error,它可以帮助您在需要时获得错误的详细视图。它可以显示最后发生的错误的完整详细信息,包括内部异常。
$Error | Get-Error
Get-Error -Newest 3 # Displays the last three errors that occurred in the session
4 ]自动(] Automatic)新版本通知
从PowerShell 7开始,系统将每天检查一次更新,并在可用时通知新版本。该信息仅在后续会话开始时显示。PowerShell 更新(PowerShell Update)提供了三个标志
- 默认(Default )GA、预览版和 RC 版本
- Off  关闭更新通知功能
- LTS仅通知长期服务 ( LTS ) GA 版本的更新
如果要在PowerShell 7(PowerShell 7)中关闭更新通知,请在PowerShell窗口中执行此命令。
$Env:POWERSHELL_UPDATECHECK = 'Off'
5]直接从PowerShell 7调用 DSC资源(Invoke DSC)
Invoke-DscResource cmdlet 运行指定PowerShell 所需状态配置(PowerShell Desired State Configuration)( DSC ) 资源的方法。这是一个实验性功能。
Using this cmdlet, configuration management products can manage Windows or Linux by using DSC resources. This cmdlet also enables debugging of resources when the DSC engine is running with debugging enabled.
6] 兼容层
它允许PowerShell用户在隐式Windows PowerShell会话中导入模块。使用它,您将能够将真正的命令作为远程计算机上的会话并将结果返回到本地会话。现在它支持导入模块,您可以在远程计算机上运行这些模块。
PowerShell也是(PowerShell)Linux和 macOS上可用的开源软件。很高兴看到PowerShell 7中有这么多新功能,我们希望它继续发展。您可以在 Microsoft 上阅读有关它的(about it on Microsoft)更多信息。
PowerShell 7 New Features List
The next major version of PоwerЅhell is out, and it brings in some significant changes. Thе Seventh version includes features like pаrallel execution, importing modules for remote exеcution, new operators, and morе. In this post, we are looking at all the New features on PowerShell 7.0.
PowerShell Directory Changes
When you install PowerShell 7, it will install to a new directory, and run along with PowerShell 5.1. If you are upgrading from PowerShell Core 6.x, then it will overwrite the PowerShell 6 directory, and remove all unnecessary files. Here is the list of directories you should know:
PowerShell 7 is installed to-
%programfiles%\PowerShell\7
The %programfiles%\PowerShell\7 folder is added to $env:PATH
The PowerShell 7 installer packages upgrade previous versions of PowerShell Core 6.x:
PowerShell Core 6.x on Windows:
%programfiles%\PowerShell\6
is replaced by
%programfiles%\PowerShell\7
Linux:
/opt/microsoft/powershell/6
is replaced by
/opt/microsoft/powershell/7
macOS:
/usr/local/microsoft/powershell/6
is replaced by
/usr/local/microsoft/powershell/7
New features in PowerShell 7
I have tried explaining each of these features in brief and how it will help PowerShell users. However, make sure to read in more detail at the Microsoft official page.
- Pipeline parallelization
- New operators
- ConciseView and Get-Error cmdlet
- Automatic new version notifications
- Invoke DSC resources directly from PowerShell 7
- Compatibility layer
Always make sure to check on Environment Variable before testing out actual codes.
1] Pipeline parallelization
You can now execute or handle objects in parallel instead of the sequence method when using ForEach-Object -Parallel. In our post on uninstalling WIM, this method can dismount three WIM images in just under 10 seconds instead of almost 25 seconds when running in sequence. Here is a sample code for it:
Get-WindowsImage -Mounted | foreach -Parallel {Measure-Command {Dismount-WindowsImage -Discard -Path $_.Path}}
2] New operators
There are three new operators—Ternary operator: a ? b : c, Pipeline chain operators: || and && and Null conditional operators: ?? and ??=. These behave like behaves like a simplified if-else statement. They make it easy to write the code instead of using the If-else loop all the time.
3] ConciseView and Get-Error cmdlet
ConciseView is a user-selectable view which is enabled as the default view of PowerShell 7. If the error is not from the script, you will receive a single line error. However, if it’s from the script or there is a parsing error, you will receive a multiline error message, and a pointer showing on which line the error occurred.
Then you have a new cmdlet Get-Error that can help you to get a detailed view of the error when desired. It can display full details, including inner exceptions, of the last error that occurred.
$Error | Get-Error
Get-Error -Newest 3 # Displays the last three errors that occurred in the session
4] Automatic new version notifications
Starting with PowerShell 7, the system will check for updates once a day and will notify about a new version if available. The information is shown only at the start of subsequent sessions. There are three flags available for PowerShell Update
- Default GA, Preview and RC releases
- Off turns off the update notification feature
- LTS only notifies of updates to long-term-servicing (LTS) GA releases
If you want to turn off update notifications in PowerShell 7, execute this command in PowerShell window.
$Env:POWERSHELL_UPDATECHECK = 'Off'
5] Invoke DSC resources directly from PowerShell 7
The Invoke-DscResource cmdlet runs a method of a specified PowerShell Desired State Configuration (DSC) resource. It is an experimental feature.
Using this cmdlet, configuration management products can manage Windows or Linux by using DSC resources. This cmdlet also enables debugging of resources when the DSC engine is running with debugging enabled.
6] Compatibility layer
It allows PowerShell users to import modules in an implicit Windows PowerShell session. Using this, you will be able to the real command as a session on the remote computer and returns the results to the local session. Now that it supports importing modules, you can run these modules on remote computers.
PowerShell is open-source software available on Linux and macOS as well. It is great to see so many new features in PowerShell 7, and we hope it keeps growing. You can read more about it on Microsoft.