我们热衷于任何使使用或管理计算机变得更容易的东西。所以很自然,我们是捷径的粉丝。Windows快捷方式和组合键(shortcuts and key combos)让一切变得更轻松。因此,微软的PowerShell快捷方式只会让PowerShell变得更好,这是合乎逻辑的。
如果您不熟悉PowerShell,我们有一份PowerShell 初学者指南(PowerShell beginner’s guide)。它非常适合家庭用户和 IT 专业人士。PowerShell不仅仅是脚本语言。这些键盘快捷键适用于 Microsoft 的Windows PowerShell ISE和PowerShell 控制台(PowerShell Console)。
尽管PowerShell正在扩展到其他操作系统(如 macOS 10.12 版和更新版本,甚至是多个Linux发行版),但这些快捷方式可能不适用于所有操作系统或所有版本的PowerShell。但是,即使不是几个小时,也可以为您节省几分钟(如果不是几个小时)来整理您的脚本。
适用于Windows PowerShell ISE的最佳键盘快捷键(Keyboard Shortcuts)
PowerShell 集成脚本环境(PowerShell Integrated Scripting Environment)( ISE ) 是您可以开发和测试PowerShell脚本和应用程序的地方。Microsoft Windows(Windows)操作系统的所有版本,无论是Windows Server、Pro还是Home,都包括PowerShell ISE。
由于Windows Powershell ISE是一个编辑环境,因此应用了最常见的Microsoft Office 快捷方式(Microsoft Office shortcuts),例如用于复制的Ctrl + C和用于粘贴的Ctrl + V。下面的快捷方式是Powershell ISE特有的。
- Ctrl + Tab 和Ctrl + Shift + Tab:将 PowerShell ISE 的焦点从一个选项卡移动到另一个选项卡。Ctrl + Tab将您带到右侧,而Ctrl + Shift + Tab将您带到左侧。
- Ctrl + T:打开一个新的PowerShell工作区。您可以使用它来分隔使用多个脚本的应用程序项目等内容。请注意(Notice)我们最初的三个脚本是如何在PowerShell 1工作区下打开的。选择PowerShell 2工作区以处理不同的项目。
- Ctrl + W:关闭当前PowerShell工作区和所有打开的脚本。如果尚未保存脚本,则会打开一个对话框询问您是否要保存更改。
- Ctrl + M:折叠或展开大纲。请注意(Notice)下面的ForEach子句以及它旁边的+ 和–符号。(– )第一张图片显示了折叠的ForEach子句;第二个显示扩展的脚本。
- Ctrl + F: 查找(Find)特定文本。此键盘快捷键可打开“查找”(Find)窗口。您将看到可以应用于搜索的参数,例如Match Case、Whole word、Search up、Find in selection。您还可以使用正则表达式(Regular expressions)。搜索从光标在脚本中的位置开始。
- F3:查找搜索参数的下一个匹配项。您可以使用“查找”窗口中的“查找(Find )下一个”(Find Next )按钮,但窗口会妨碍您。尝试选择F3以跳转到下一个匹配项。它将具有光标突出显示。
- Shift + F3: 如果您使用 F3 超出了您需要的范围怎么办?只需(Simply)使用Shift + F3即可查找上一个匹配项。它也将突出显示光标。
- Ctrl + H: 查找(Find)s 并将脚本中的任何文本替换为其他文本。您可以将与查找(Find)工具相同的参数应用于此工具。
- Ctrl + J:打开片段选择框。片段(Snippets)是具有正确语法的小段代码。双击(Double-click)以插入片段,然后添加您的参数。
- Ctrl + Space:与显示片段类似,此命令打开Intellisense。Intellisense 提供特定于上下文的选项,例如参数或比较器。在这种情况下,它显示了Remove-Item(Remove-Item) cmdlet的-ErrorAction参数的可能值。单击(Click)一个以选择它。
- F5:运行整个脚本并提示保存。选中以后不再显示此消息(In the future, do not show this message)旁边的框,然后单击确定(OK )以在以后按 F5 运行并保存。这与选择“运行脚本”(Run Script )按钮相同。
- F8:选择PowerShell脚本的一部分,然后按F8仅运行该片段。这有助于一次调试一个脚本。这与选择“运行选择”(Run Selection )按钮相同。
- Ctrl + C或Ctrl + Break:在脚本运行时停止脚本。如果您选择了一些文本,Ctrl + Break将在该点停止操作。这是另一个有助于调试脚本的快捷方式。选择停止操作(Stop Operation )按钮具有相同的效果。
Windows PowerShell 控制台(Windows PowerShell Console)的最佳键盘快捷键(Keyboard Shortcuts)
如果您使用过命令提示符(Command Prompt)(cmd),您会发现Windows PowerShell 控制台(Windows PowerShell Console)只是为在命令行中使用PowerShell脚本和 cmdlet 而量身定制的命令提示符。控制台看起来很像旧的命令行控制台。
如果您不制作自动化应用程序或 Windows 服务器管理包,则可以使用控制台运行一些PowerShell命令。但是,PowerShell控制台还有更多功能和快捷方式。常用的命令控制台快捷方式,如向上箭头 ( ^ ) 和向下箭头 ( ˅ ),用于调用最近的命令,也可以在PowerShell控制台中使用。
- Letter + F8:不记得您最近使用的那个 cmdlet,但您知道它以 S 开头?Press S + F8列出所有最近使用的以字母 S 开头的 cmdlet。在下图中,看到第一个字母是不同的颜色,让我们知道我们刚刚在 S 上搜索过。
- Alt + F7: 从控制台的历史记录中清除所有最近的命令。(Clears)这就像清除剪贴板(clearing the clipboard)一样。当您在控制台中完成大量操作时,这很有帮助,并且向上和向下箭头不再有助于在所需命令之间跳转。
- Shift + Enter:要输入多行命令,请在每行末尾使用Shift + Enter ,然后再输入下一行。(Enter )然后选择Enter以依次运行它们。
- F8: 搜索(Search)命令历史记录中以提示符开头的项目。在下面的示例中,Get是在命令提示符下键入的。再次选择 F8 将找到下一个以Get(Get)开头的命令(如果有的话)。
- Ctrl + Space:与(Space:)Intellisense类似,但在控制台中。在示例中,Get-Item是输入的全部内容。使用Ctrl + Space会显示其他具有该开头的 cmdlet 及其语法。您可以使用左右箭头在 cmdlet 之间切换。
- 选项卡:(Tab:)类似于自动完成功能,但通过反复按下它来循环显示各种可能性。在第一个示例中,参数只有破折号 ( – ) 开头。选择选项卡会遍历选项,直到获得所需的选项。在这种情况下,它是-Recurse。
您现在拥有PowerShell的(PowerShell)力量(Power)
当微软(Microsoft)在 2006 年发布PowerShell时,IT 专业人员正忙于使用图形界面和批处理文件来管理事物。现在,借助PowerShell(PowerShell),这些自动化和配置方法的处理工作更少、速度更快。
The 19 Best Windows Powershell Keyboard Shortcuts
We’re fans of anything that makes using or managing computers easier. So naturally, we’re fans of shortcuts. Windows shortcuts and key combos make everything easier. It’s logical then that Microsoft’s PowerShell shortcuts only make PowerShell better.
If you’re not familiar with PowerShell, we’ve got a PowerShell beginner’s guide. It’s great for home users and IT pros alike. There’s more to PowerShell than just the scripting language. These keyboard shortcuts apply to Microsoft’s Windows PowerShell ISE and PowerShell Console.
Even though PowerShell is spreading to other operating systems—like macOS version 10.12 and newer and even several Linux distributions— these shortcuts may not work on all operating systems or all versions of PowerShell. But even a few will save you minutes, if not hours, putting together your scripts.
Best Keyboard Shortcuts for Windows PowerShell ISE
The PowerShell Integrated Scripting Environment (ISE) is where you can develop and test PowerShell scripts and apps. All versions of Microsoft’s Windows operating system, whether Windows Server, Pro, or Home, include the PowerShell ISE.
Since Windows Powershell ISE is an editing environment, most common Microsoft Office shortcuts apply, like Ctrl + C for copy and Ctrl + V for paste. The shortcuts below are particular to the Powershell ISE.
- Ctrl + Tab and Ctrl + Shift + Tab: Moves the PowerShell ISE’s focus from tab to tab. Ctrl + Tab takes you to the right, and Ctrl + Shift + Tab takes you left.
- Ctrl + T: Opens a new PowerShell workspace. You could use this to separate things like app projects utilizing multiple scripts. Notice how our original three scripts are open under the PowerShell 1 workspace. Select PowerShell 2 workspace to work on a different project.
- Ctrl + W: Closes the current PowerShell workspace and all open scripts. If a script isn’t saved yet, a dialog will open to ask if you want to save the changes or not.
- Ctrl + M: Collapse or expand outlining. Notice the ForEach clause below and the + and – signs next to it. The first image shows the ForEach clause collapsed; the second shows the script expanded.
- Ctrl + F: Find specific text in the script. This keyboard shortcut opens the Find window. You’ll see parameters that you can apply to your search, such as Match Case, Whole word, Search up, Find in selection. You can also use Regular expressions. The search begins where the cursor is placed in the script.
- F3: Finds the next occurrence of the search parameters. You could use the Find Next button in the Find window, but the window gets in the way. Try selecting F3 to jump to the next occurrence. It will have cursor highlighting.
- Shift + F3: What if you go past what you need using F3? Simply use Shift + F3 to find the previous occurrence. It will have cursor highlighting as well.
- Ctrl + H: Finds and replaces any text in the script with other text. You can apply the same parameters to this tool as the Find tool.
- Ctrl + J: Opens a snippets selection box. Snippets are small pieces of code with the proper syntax. Double-click to insert a snippet, then add your parameters.
- Ctrl + Space: Similar to showing snippets, this command opens Intellisense. Intellisense presents context-specific options like parameters or comparators. In this case, it shows the possible values of the -ErrorAction parameter for the Remove-Item cmdlet. Click on one to select it.
- F5: Runs the entire script and prompts to save it. Check the box next to In the future, do not show this message, and then OK to run and save by pressing F5 in the future. It’s the same as selecting the Run Script button.
- F8: Select a section of the PowerShell script and press F8 to run just that snippet. This helps debug the script one piece at a time. It’s the same as selecting the Run Selection button.
- Ctrl + C or Ctrl + Break: Stops the script while it’s running. If you have some text selected, Ctrl + Break will stop the operation at that point. This is another shortcut helpful for debugging your scripts. Selecting the Stop Operation button has the same effect.
Best Keyboard Shortcuts for the Windows PowerShell Console
If you’ve used the Command Prompt (cmd), you’ll find that the Windows PowerShell Console is just a command prompt tailored for using PowerShell scripts and cmdlets at the command line. The console looks a lot like the old command-line console.
If you’re not crafting automation apps or Windows server management packages, you can use the console to run a few PowerShell commands. However, the PowerShell console has a few more functions and shortcuts. The usual command console shortcuts like the up arrow (^) and down arrow (˅) to recall recent commands also work in the PowerShell console.
- Letter + F8: Can’t remember that cmdlet you used recently, but you know it started with an S? Press S + F8 to list all recently used cmdlets beginning with the letter S. In the image below, see that the first letter is a different color to let us know we just searched on S.
- Alt + F7: Clears all the recent commands from the console’s history. It’s like clearing the clipboard. This is helpful when you’ve done a lot in the console, and the up and down arrows are no longer helpful to jump between the needed commands.
- Shift + Enter: To enter several lines of commands, use Shift + Enter at the end of each line before entering the next one. Then select Enter to run them all in sequence.
- F8: Search through items in the command history that start with what’s at the prompt. In the example below, Get was typed at the command prompt. Selecting F8 again would find the next command that begins with Get if there is one.
- Ctrl + Space: Works like Intellisense but in the console. In the example, Get-Item was all that was entered. Using Ctrl + Space shows other cmdlets with that beginning and their syntax. You can use the left and right arrows to go between the cmdlets.
- Tab: Works like autocomplete but cycles through the possibilities by pressing it repeatedly. In the first example, there’s only the dash (–) beginning of a parameter. Selecting tab goes through the options until you get the one you want. In this case, it’s -Recurse.
You Have the Power of PowerShell Now
When Microsoft released PowerShell in 2006, IT professionals were busy using graphical interfaces and batch files to manage things. Now those methods of automation and configuration are being handled with less work and quicker, thanks to PowerShell.