PowerShell为您提供了对 Windows PC 的所有控制,而且它易于学习。所以放弃命令提示符(Command Prompt)并使用Windows PowerShell命令来自动执行这些重复的管理任务。
学习PowerShell还将教您如何在 bash shell中处理大多数Linux管理任务。(Linux)PowerShell与您在大多数Linux 发行版(Linux )中(Distros)找到 的类似。
什么是 PowerShell?
Windows PowerShell是Microsoft为系统管理员开发的命令行界面和脚本语言。但是您不必是管理员或程序员也可以使用它,因为它比其他编程语言简单得多。它类似于普通英语(English),每个人都可以理解并使用它来管理他们的计算机。
Windows PowerShell中使用的命令称为“cmdlet”。它们可以让您激活计算机上的特定操作或一系列操作。这些操作可以很简单,例如登录无线网络,也可以更复杂,例如自动执行特定的重复性任务。
要打开 PowerShell,请按开始(Start )按钮,在搜索框中键入“ powershell ”,然后选择(powershell)Windows PowerShell。您将打开命令行界面,您可以在其中开始输入命令。
在本文中,我们将介绍十个最有用和最简单的PowerShell命令。
1. 获得帮助
Get-Help cmdlet 是在(Get-Help)PowerShell中开始的好地方。它概述了您可以做什么,并且在您需要有关特定 cmdlet 的更多信息时也非常有用。
只需(Simply)打开PowerShell窗口并键入Get-Help。这将打开一个全面的概述,它将教您什么是PowerShell,并且您将了解它的功能和结构。您还可以直接通过界面访问 Web 以获取更高级的帮助文件。您可以下载这些文件以备后用。
但这并不是 get-help 命令的结束。类型:
Get-Help *
PowerShell将列出您当时可以使用的所有 cmdlet。该列表可能很大,您无需了解所有显示的 cmdlet。但是请随意阅读它们并探索所有可用的可能性。
类型:
Get-Help <command name>
您不需要括号,只需键入您感兴趣的命令的名称,此 cmdlet 将为您提供有关它的基本信息。有关命令的更详细说明,请键入:
Get-Help <command name> -Full
如果您需要一个如何使用特定命令类型的示例:
Get-Help <command name> -Example
2.获取命令
您想快速检索有关特定命令或命令集的信息吗?Get-Command cmdlet 将为您执行此操作。Get-Command中有很多选项,但作为PowerShell的基本用户,您不需要其中的大部分。以下是一些有用的基本 cmdlet,尤其是在导航可用命令时。
如果您想要相同类型的特定 cmdlet 列表:
Get-Command -<CommandType>
例如,这个复杂的短语将命令您的计算机显示所有可用 cmdlet 类型命令的列表,并按名称中名词的字母顺序对它们进行排序:
Get-Command -Type Cmdlet | Sort-Object -Property Noun| Format-Table -GroupBy Noun
这是一个让PowerShell执行复杂任务的更复杂命令的绝佳示例。一旦你理解了语法,你就会发现使用PowerShell通常是在你的计算机上完成某些事情的最快和最简单的方法。
如果要按名称查找特定命令,只需键入:
Get-Command -Name <name of command>
如果您不知道要查找的命令的确切名称,但您知道名称的一部分,您可以键入:
Get-Command -Name <name of command> -UseFuzzyMatching
此 cmdlet 将要求您的计算机查找与您在 <name> 字段中键入的内容非常匹配的命令。如果您容易出现拼写错误并且不想一直被纠正自己所困扰,您也可以使用此 cmdlet。
3. 获取内容
如果要查看文件的内容,请使用 Get-Content cmdlet。您将需要指定所述文件的确切位置,以便您的计算机可以找到它。
在上面的示例中,您可以看到为此目的制作的测试文本文件的内容。这是一个简单的命令,但如果您知道它的作用,您可以将它与其他命令结合使用,并通过Windows PowerShell执行非常强大的任务。
4. 获取进程
Get-Process cmdlet 将为您提供有关计算机上当前正在运行的进程的信息。您可以查看内存使用情况、CPU周期、进程 ID 和其他属性。
如果将进程名称添加到此 cmdlet,则可以查看有关特定进程的信息。例如,如果您想要有关Discord应用程序流程的信息,只需键入Get-Process Discord。
您只需替换语法的“Get”前缀即可通过PowerShell启动或停止进程。(PowerShell)要启动进程,请键入:
Start-Process <process name or ID> Â Â
要停止进程,请键入:
Stop-Process <process name or ID>
5. 获取服务
作为系统管理员,您可能会发现Get-Service cmdlet 非常有用。这个简单的 cmdlet 将显示系统上当前运行的所有服务,并允许您控制它们。这意味着您可以看到当前正在运行以使您的计算机运行的所有程序。
现在您可以使用服务名称来控制它。使用命令如Restart-Service <service name>来解开冻结的那些,Stop-Service <service name>完全停止它们,Start-Service重新启动它们。
6. 获取物品
如果您需要特定项目,通过PowerShell(PowerShell)找到它可能比使用集成到您的操作系统中的搜索栏更快。如果您知道所需文件的位置,则特别方便。只需(Simply)以这种方式使用命令:
获取项目 C:(Get-Item C:)
这将为您带来“C:”硬盘驱动器中的文件列表。您可以更具体地搜索用户配置文件下的所有目录,或者深入“文档”或“下载”目录。为此,请键入:
Get-Tem C:\users\<user name>\*
如果需要从容器(例如系统目录)中检索子项,可以使用 cmdlet Get-ChildItem。此命令将显示该容器内的所有文件和目录。与Get-Item cmdlet 一样,您需要指定容器的路径。
要删除项目,您可以将“Get”后缀替换为“Remove”。这将允许您删除所有内容,从文件和文件夹到注册表项和功能。键入Remove-Item <location\ name>。您可以详细说明此 cmdlet 并仅删除隐藏和“只读”文件或从特定文件夹和子文件夹中删除项目。
7. ConvertTo-Html
此命令将帮助您将 .NET 对象转换为可在Web浏览器中显示的HTML文件。这是一个输出 cmdlet,这意味着它可以很好地与其他 cmdlet 配合使用。只需(Simply)将其添加到其他 cmdlet 的末尾并包含特定的文件路径。
例如,如果您想创建一个HTML文件来显示本地计算机所有当前进程的名称、路径和公司,请键入:
Get-Process | ConvertTo-Html -Property Name, Path, Company -Title “Process Information” | Out-File filename.htm
将(Replace)filename.htm 替换为您想要的任何文件名。
要打开创建的文件,请使用Invoke-Item cmdlet加上 <file name.htm>。内容应类似于下图。
8. 设置执行策略
如果要运行本地PowerShell脚本,则必须更改PowerShell本身的执行策略。在Windows计算机上,执行策略始终受到限制,您需要将其更改为RemoteSigned或Unrestricted。在Linux(Linux)或Mac等非Windows计算机上,它通常设置为Unrestricted,并且无法 更改。
如果您想了解有关PowerShell执行策略的更多信息,只需键入:
帮助设置执行策略(help Set-ExecutionPolicy)
PowerShell将在您的计算机上显示有关执行策略的帮助文件,如果没有则通知您。但它也会为您提供一个链接,您可以关注并在线阅读该链接。
要将策略设置为RemoteSigned,只需键入:
设置执行策略 RemoteSigned(Set-Executionpolicy RemoteSigned)
要将其设置为无限制类型:
Set-Executionpolicy Unrestricted
要检查执行策略的当前设置,请键入:
获取执行策略(Get-Executionpolicy)
因为执行策略是 PowerShell 安全策略的一部分,所以它会要求您确认更改。仅当您确定要继续时才键入Y。
9. 选择对象
此命令用于选择对象和对象集、唯一对象以及数组中特定位置的对象。您可以使用不同的参数来指定要选择的对象。其中包括属性、数值和特定的输入对象。
要按属性属性选择对象,您需要指定名称、ID 或CPU等属性。您甚至可以对属性进行分组,但在键入命令时必须用逗号分隔它们。要在操作类型中查看 Select-Object cmdlet:
Get-Process | Select-Object -Property ProcessName, Id, CPU
PowerShell将列出所有当前进程及其属性,如下所示。
您现在可以使用 -EpandProperty 命令获取有关特定属性的详细信息。如果您需要有关ProcessName属性类型的(ProcessName)Modules属性的详细信息:
Get-Process Chrome | Select-Object -Property ProcessName -ExpandProperty Modules | Format-list
该命令的“Format-list”输出用于使结果格式化和可读。
10. 导出-CSV
有时PowerShell命令会产生很长的列表,正如我们在(PowerShell)Get-Command cmdlet 中看到的那样。长列表和大量信息难以阅读,您会希望以更有条理的方式阅读。为此,请将所有数据导出到CSV文件中。您可以使用Export- CSV函数直接在PowerShell中执行此操作。
例如,我们看到 cmdlet Get-Command为我们提供了一个很长的列表,其中列出了当时我们可以在PowerShell中使用的所有可用 cmdlet。(PowerShell)您可以结合使用Get-Command和Export-CSV来创建一个包含逗号分隔值的详细列表,例如Excel等应用程序可以识别 的名称、来源和版本。
为此,请在PowerShell命令提示符中键入以下命令,但请注意,您可以设置所需的任何路径,并且可以以不同的方式命名文件:
Get-Command | Select Name, Source, Version | export-csv D:\AllCommands.csv
您现在可以在Excel中打开列表,您应该会看到类似于下图的内容。
10 Easy PowerShell Commands All Windows Users Should Know
PowerShell gives you all the control you need over your Windows PC, and it’s easy to learn. So ditch Command Prompt and use Windows PowerShell commands to automate those repetitive administrative tasks.
Learning PowerShell will also teach you how to handle most Linux administrative tasks inside the bash shell. PowerShell is similar to what you find in most Linux Distros.
What Is PowerShell?
Windows PowerShell is a command-line interface and scripting language developed by Microsoft for system administrators. But you don’t have to be an admin or programmer to use it because it is much simpler than other programming languages. It is similar to regular English, and everyone can understand it and use it to administer their computers.
The commands used in Windows PowerShell are known as “cmdlets.” They will let you activate a specific action, or series of actions, on your computer. These actions can be simple, like logging into a wireless network, or more complex, like automating specific repetitive tasks.
To open PowerShell, press the Start button, type “powershell” into the search box, and select Windows PowerShell. You’ll open to the command-line interface, where you’re ready to start typing commands.
In this article, we’ll go over ten of the most useful and easy PowerShell commands.
1. Get-Help
The Get-Help cmdlet is a great place to start in PowerShell. It provides an overview of what you can do and is also great when you need more information on a particular cmdlet.
Simply open the PowerShell window and type Get-Help. This will open up a comprehensive overview that will teach you what PowerShell is, and you will understand its functionality and structure. You also can access the web for more advanced help files directly through the interface. You can download these files for later use.
But that’s not the end of the get-help command. Type:
Get-Help *
PowerShell will list all the cmdlets you can use at that moment. The list will probably be huge, and you don’t need to learn all the displayed cmdlets. But feel free to read them and explore all the possibilities available to you.
Type:
Get-Help <command name>
You don’t need the brackets, simply type the name of the command you are interested in, and this cmdlet will give you the basic info about it. For an even more detailed explanation of a command, type:
Get-Help <command name> -Full
If you need an example of how to use a certain command type:
Get-Help <command name> -Example
2. Get-Command
Do you want to retrieve information about a specific command, or set of commands, quickly? The Get-Command cmdlet will do that for you. There are so many options within Get-Command, but you, as a basic user of PowerShell, don’t need most of them. Here are some basic cmdlets that will be helpful, especially when navigating the available commands.
If you want a list of specific cmdlets of the same type:
Get-Command -<CommandType>
For example, this complex phrase will order your computer to display a list of all cmdlet type commands available and to sort them by alphabetical order of the noun in their name:
Get-Command -Type Cmdlet | Sort-Object -Property Noun| Format-Table -GroupBy Noun
This is an excellent example of a more complex command that will make PowerShell execute a complicated task. Once you understand the syntax, you will see that using PowerShell is often the fastest and easiest to get some things done on your computer.
If you want to find a specific command by its name, simply type:
Get-Command -Name <name of command>
If you don’t know the exact name of the command you’re looking for, but you know part of the name, you can type:
Get-Command -Name <name of command> -UseFuzzyMatching
This cmdlet will task your computer to find commands that closely match what you typed in the <name> field. You also can use this cmdlet if you are prone to misspellings and don’t want to be bothered with correcting yourself all the time.
3. Get-Content
If you want to see the content of a file, use the Get-Content cmdlet. You will need to specify the exact location of the said file so your computer can find it.
In the example above, you can see the content of a test text file made for this purpose. This is a straightforward command, but if you know what it does, you can combine it with other commands and perform really powerful tasks through Windows PowerShell.
4. Get-Process
The Get-Process cmdlet will give you information on currently running processes on your computer. You can see the memory usage, CPU cycles, process ID, and other attributes.
If you add the name of the process to this cmdlet, you can see the information on a specific process. For example, if you want information on the Discord application process, simply type Get-Process Discord.
You can start or stop processes through PowerShell by simply replacing the “Get” prefix of the syntax. To start a process, type:
Start-Process <process name or ID>
To stop a process, type:
Stop-Process <process name or ID>
5. Get-Service
As a system administrator, you may find the Get-Service cmdlet very useful. This simple cmdlet will display all the services currently running on your system and allow you to control them. This means you can see all the programs currently working to make your computer run.
Now you can use the service name to control it. Use commands such as Restart-Service <service name> to unstick the frozen ones, Stop-Service <service name> to stop them completely, and Start-Service to start them again.
6. Get-Item
If you need a specific item, it might be faster to find it through PowerShell than by using a search bar integrated into your operating system. It is especially convenient if you know the location of the file you need. Simply use the command in this way:
Get-Item C:
This will bring you the list of files in the “C:” hard drive. You can be even more specific and search in all the directories under the user’s profile or go deeper into the “documents” or “downloads” directories. To do this, type:
Get-Tem C:\users\<user name>\*
If you need to retrieve a child item from a container, such as a system directory, you can use the cmdlet Get-ChildItem. This command will show you all the files and directories inside that container. As with the Get-Item cmdlet, you will need to specify the container’s path.
To delete items, you can replace the ”Get” suffix with “Remove.” This will allow you to delete everything, from files and folders to registry keys and functions. Type Remove-Item <location\ name>. You can elaborate this cmdlet and delete only hidden and “read-only” files or delete items from specific folders and subfolders.
7. ConvertTo-Html
This command will help you convert a .NET object into an HTML file that can be displayed in Web browsers. This is an output cmdlet which means that it works great with other cmdlets. Simply add it to the end of other cmdlets and include a specific file path.
For example, if you want to create an HTML file that displays the name, path, and the company of all the current processes of a local computer, type:
Get-Process | ConvertTo-Html -Property Name, Path, Company -Title “Process Information” | Out-File filename.htm
Replace filename.htm with any file name you want.
To open the created file, use the Invoke-Item cmdlet plus <file name.htm>. The contents should look something like the image below.
8. Set-Executionpolicy
If you want to run local PowerShell scripts, you will have to change the execution policy of the PowerShell itself. On Windows computers, the execution policy is always restricted, and you will need to change it to RemoteSigned or Unrestricted. On non-Windows computers such as Linux or Mac, it is typically set to Unrestricted, and it can’t be changed.
If you want to learn more about the PowerShell execution policy, simply type:
help Set-ExecutionPolicy
The PowerShell will display the help files about execution policy on your computer or notify you if there are none. But it will also provide you with a link that you can follow and read about it online.
To set the policy to RemoteSigned, simply type:
Set-Executionpolicy RemoteSigned
To set it to Unrestricted type:
Set-Executionpolicy Unrestricted
To check the current setting for the execution policy, type:
Get-Executionpolicy
Because execution policies are part of PowerShell’s security strategies, it will ask you for confirmation of the change. Type Y only if you are sure you want to proceed.
9. Select-Object
This command is used to select objects and sets of objects, unique objects, and objects in a specific position in an array. There are different parameters that you can use to specify the objects you want to select. Among them are properties, numeric values, and specific input objects.
To select objects by property attribute, you will need to specify the attributes such as name, ID, or CPU. You can even group the attributes, but you must separate them with commas when typing in the command. To see the Select-Object cmdlet in action type:
Get-Process | Select-Object -Property ProcessName, Id, CPU
The PowerShell will list all the current processes and their attributes, as shown below.
You can now use the -EpandProperty command to get detailed information about the specific attribute. If you want details about the Modules property of the ProcessName attribute’s type:
Get-Process Chrome | Select-Object -Property ProcessName -ExpandProperty Modules | Format-list
The “Format-list” output of the command is there to make the results formatted and readable.
10. Export-CSV
Sometimes the PowerShell commands result in long lists, as we saw with the Get-Command cmdlet. Long lists and lots of information are difficult to read, and you will want it in a more organized manner. To do this, export all the data into a CSV file. You can do this directly in PowerShell by using the Export-CSV function.
For example, we saw that the cmdlet Get-Command gives us a long list of all available cmdlets we can use in PowerShell at that moment. You can combine the Get-Command and Export-CSV to create a detailed list with comma-separated values such as name, source, and version that apps like Excel can recognize.
To do this, type the following command into the PowerShell command prompt but note that you can set any path you want, and you can name your file differently:
Get-Command | Select Name, Source, Version | export-csv D:\AllCommands.csv
You can now open the list in Excel, and you should see something similar to the following image.