Detailed Windows10 common PowerShell advanced tasks

  
                

Windows PowerShell is a command-line shell and scripting environment that will likely replace the command prompt (CMD) in the future. In the Windows 10 system, many users are relatively unfamiliar with PowerShell, so today Xiaobian introduces 15 common PowerShell advanced tasks.

15 PowerShell advanced tasks commonly used in Windows 10:

1, open the PowerShell environment

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

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

2, set the date and time

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

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 directly specifying the specific value, to complete this task or use the Set-Date cmdlet , but the usage is different from the previous ones, for example:

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

From the above command you can see that we first pass Get -Date Gets the current date and then triggers Set-Date to add 2 to the date. Of course, this command can also use AddHours, AddMinutes or AddSeconds to increase the time, minute, second, etc. for the time.

4, verify files and folders

PowerShell command can easily check whether a file and folder exist on the computer, use the Test-Path cmdlet and then follow the path to complete the verification. No need to spend time looking in the resource manager. For example, to verify that the C drive has an Excel file named PowerShell.xlsx, you can use the following command:

Test-Path c:\\PowerShell.xlsx

This command returns True or False such as False, if you don't know the exact file name, you can also use wildcards directly, for example:

Test-Path c:\\*.xlsx

5. Rename the file and Folders

Once you know the exact path to the filename folder, PowerShell can also easily rename files and folders. Just use the Rename-Item cmdlet with the following example:

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

6. Moving files and folders

It's also very easy to move files or folders using PowerShell, using the Move-Item cmdlet , for example:

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

Combine wildcards to quickly move certain types of files from one folder to another Clip:

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

7. Open the program

The Invoke-Item cmdlet can open the application directly from the PowerShell prompt:

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

However, applications that are already in the Windows Path path can be executed directly by name, for example:

notepad

8. Using the default Program Open File

The Invoke-Item cmdlet can be used to open a file directly, in addition to executing the application. But everyone needs to be aware that when you use it to open a file, it will only be opened with the default application associated with that file type.

Invoke-Item c:\\Sysgeek\\Hello.txt

9. Open the file as a batch

When the Invoke-Item cmdlet is combined with a wildcard, you can open it in batches A type of file:

Invoke-Item c:\\Sysgeek\\*.txt

10, read text file

PowerShell can directly handle the contents of text files, For example, you can read the contents of a text file using the Get-Content command:

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

If you just want to preview the file instead of reading the entire text, You can use the -totalcount parameter:

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

11. Add text content

In addition to reading text file content Use the Add-Content cmdlet in PowerShell to add content directly:

Add-Content c:\\Sysgeek\\Hello.txt "by Sea Monkey"

Of course, this command will only Appending text to the end of the file does not necessarily meet your needs.

12, statistical text files

Whether you want to count ordinary files, or want to count how many lines of code written in a day, you can use the following command:

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

Copyright © Windows knowledge All Rights Reserved