PowerShell: Detailed explanation of advanced PowerShell tasks commonly used in Windows 10

Detailed explanation of advanced PowerShell tasks commonly used in Windows 10

2018-03-19 10:40  Source: Other  Authors: Anonymous   0

   Disclaimer: This article is not original  

Windows PowerShell is a command-line shell and scripting environment that will likely replace Command Prompt (CMD) in the future. In the Windows 10 system, many users are relatively unfamiliar with PowerShell, so today I will introduce 15 commonly used PowerShell advanced tasks.

  15 advanced PowerShell tasks commonly used in Windows 10:

  1. Open the PowerShell environment

  Windows 10 has a built-in PowerShell command line environment, you can directly search for "PowerShell" and run it as an administrator.

  Or type PowerShell in the search bar and open it with Ctrl + Shift + Enter.

  2. Set date and time

  There are many ways to set the time and date of the Windows system, but using PowerShell should be the fastest way, just like the following commands:

  Set-Date -date “2016-12-01 8:30 AM”

  AM and PM believe that there is no need to explain it.

  3. Adjust the date and time

  In some special cases, we may need to adjust the date and time instead of specifying the specific value directly. To accomplish this task, we still use the Set-Date cmdlet, but the usage is different from before, for example:

  Set-Date (Get-Date).AddDays(2)

  从上面命令大家可以看到,我们先通过 Get-Date 获取当前日期之后再触发 Set-Date 为日期加 2。当然此命令还可以使用 AddHours、AddMinutes 或 AddSeconds 为时间增加时、分、秒等。

  4、验证文件和文件夹

  PowerShell 命令可以方便地检查计算机上是否存在某个文件和文件夹,使用 Test-Path cmdlet 再跟上路径即可完成验证,而无需花时间在资源管理器中去找。例如:要验证 C 盘是否存在一个名为 PowerShell.xlsx 的 Excel 文件可以使用如下命令:

  Test-Path c:\PowerShell.xlsx

  此命令返回的是 True 或 False 这样的布尔值,如果你不知道确切的文件名,也可以直接使用通配符,例如:

  Test-Path c:\*.xlsx

  5、重命名文件和文件夹

  一旦你知道文件名文件夹的确切路径,PowerShell 也可以非常容易地重命名文件和文件夹,只需参考如下示例使用Rename-Item cmdlet 即可:

  Rename-Item c:\PowerShell.xlsx New_PowerShell.xlsx

  6、移动文件和文件夹

  使用 PowerShell 移动文件或文件夹也非常容易,使用 Move-Item cmdlet 即可,例如:

  Move-Item c:\PowerShell.xlsx d:\PowerShell.xlsx

  结合通配符使用,可以快速将特定类型的文件从一个文件夹移动到另一个文件夹:

  Move-Item c:\*.xls d:\excel\

  7、打开程序

  Invoke-Item cmdlet 可以直接在 PowerShell 提示符中打开应用程序:

  Invoke-Item c:\Windows\System32\notepad.exe

  但已经在 Windows Path 路径中的应用可以直接用名称执行,例如:

  notepad

  8、使用默认程序打开文件

  Invoke-Item cmdlet 除可以执行应用程序外,还可直接用于打开文件。但需要大家注意的是,使用它打开文件时,只会使用该文件类型关联的默认应用程序打开。

  Invoke-Item c:\Sysgeek\Hello.txt

  9、以批处理形式打开文件

  当 Invoke-Item cmdlet 与通配符结合时,可以批量打开某类型的文件:

  Invoke-Item c:\Sysgeek\*.txt

  10、读取文本文件

  PowerShell 是可以直接处理文本文件内容的,例如使用 Get-Content 命令即可读取文本文件内容:

  Get-Content c:\Sysgeek\Hello.txt

  如果你只是想预览文件,而非阅读整个文本,可以使用 -totalcount 参数:

  Get-Content c:\Sysgeek\Hello.txt -totalcount 1

  11、添加文本内容

  除读取文本文件内容外,在 PowerShell 中使用 Add-Content cmdlet 可以直接添加内容:

  Add-Content c:\Sysgeek\Hello.txt "by 海猴子"

  当然,此命令只是将文本追加到文件最后,不一定满足你的需求。

  12、统计文本文件

  不论是要统计普通文件也好,还是想统计一天编写了多少行代码,都可以使用如下命令:

  Get-Content c:\Sysgeek\Hello.txt | Measure-Object

  13、服务状态统计

  当你需要统计 Windows 服务及状态时,此前可能会手动一个一个去查看,其实使用 PowerShell 的 Get-Service cmdlet 即可立即遍历出当前系统上的所有服务及状态:

  Get-Service

  除此之外,要按服务状态进行统计也非常方便,例如要统计当前所有已停止的服务可以使用如下命令:

  Get-Service | Where-Object {$_.status -eq "stopped"}

  14. Restart the service

  Once you've determined that a service is causing the problem, you can restart it directly from PowerShell:

  Restart-Service Dnscache

  If you don't know the specific service name, you can also specify the display name of the service through parameters:

  Restart-Service -displayname "DNS Client"

  15. Change the service startup status

  Changing the startup state of a service in PowerShell is also as simple as using the -startuptype parameter of Set-Service:

  Set-Service Dnscache -startuptype "manual"

  This parameter can also be replaced by two parameters, automatic (automatic start) and disabled (disabled).

  Force Refresh Windows 10 Apps

  If your Windows 10 Apps are stuck on the startup screen or fail to initialize, you can use the following PowerShell command to force an app refresh:

  Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

  In addition to that, PowerShell can also be used to uninstall Windows 10 built-in apps.

  Here are 15 advanced PowerShell tasks that are commonly used in Windows 10. This is a very practical article. You can save it first, and you can check it if you forget the method in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326163112&siteId=291194637