Powershell is the next-generation scripting language built by Microsoft. It is based on the. NET Framework and is powerful and easy to expand. It is integrated with many products of Microsoft and its third-party companies. Windows 7 and Windows Server 2008 R2 have built-in powershell 2.0. Other operating systems must be downloaded separately.HereDownload.
Sample script download
All scripts in this series are tested and passed in Windows Server 2008 R2 (powershell 2.0) + powergui Script Editor.
Powershell development tools:
1. Windows powershell ise: a lightweight power shell ide provided by Microsoft, which is stored in
% SystemRoot % \ windows \ system32 \ windowspowershell \ V1.0
SupportedCodeColoring, breakpoint debugging, etc., but does not support intelligent sensing:
2. powergui Script Editor: an excellent ide developed by quest. It is divided into the free version and Professional Edition,Official homepage.
Convenient smart sensing and syntax prompts also support breakpoint debugging:
I will use the free powergui Script Editor for testing.
3. powershell plus: Another powerful IDE, commercial software,Official homepage.
Recommended resources:
Technet powershell Homepage
Technet powershell Script Center
Chinese powershell V2 blog, Very deep, updated every month, recommended for reading.
One powershellCommunity
Powergui community, Including many extensions, examples, and blogs.
Idera powershell Homepage, Including many extensions, examples, and blogs.
Microsoft powershell official blog
Next, let's take a look at the application of powershell in the file system.
Powershell does not have the file concept, but only the item [item] concept. information such as Windows Drive, directory, file, registry, and variable is abstracted into the "item" concept, powershell uses "items" for unified management. This time, we only focus on the file system, that is, directories and files. We will learn more about the subsequent chapters of other systems.
The file system commands are as follows:
New-item
Copy-item
Move-item
Remove-item
Rename-item
Invoke-item
Of course, these commands are not only used in file systems, such as drives and registries. This time, we only pay attention to these commands from the perspective of file systems.
- New-item: Creates a file or directory at a specified location.
1.1Create a directory:
New-item
-Path
E: \ pstest \ newdir
-Itemtype
Directory
-Force
You have created an edisk.Pstest \ newdirDirectory structure,ItemtypeIndicates the type of the created item. Here is the directory"Directory",ForceIndicates that if the directory structure exists, it overwrites.
1.2Create an object:
New-item
-Path
E: \ pstest \ newdir \ new.txt
-Itemtype
File
-Force
InE: \ pstest \ newdirCreates a new.txt text file. If the itemtype is file, it overwrites the new.txt text file.
There are also many parameters, such as Whatif: predict what will happen if the command is executed. For details about the parameter list and its usage, see the powershell documentation. here only the most common parameters are listed.
2. Copy-item: Copy files and directories.
2.1 copy a file:
Copy-item
-Path
E: \ pstest \ newdir \ new.txt
-Destination
D :\
-Passthru
-Force
Copy a file from a directory on the E disk to the D disk. If yes, overwrite the file. Passthru forces powershell to return the result after command execution to check whether the command is successfully executed. It can be seen that move-item supportsCross-driveCopy a file.
2.2 copy directory:
Copy-item
-Path
E: \ pstest \ newdir
-Destination
D :\
-Passthru
-Force
-Recurse
Copy a directory in disk e to disk D and overwrite it if it exists. Note that the "recurse" parameter is used only when this parameter is added. powershell copies all subdirectories and their files in the source directory. Otherwise, only one shell is copied to the source directory.
3. Move-item: Move files and directories, which are cut.
3.1. Move files:
Move-item
-Path
E: \ pstest \ newdir \ new.txt
-Destination
E :\
-Passthru
-Force
Move a file to the root directory. If it exists, it overwrites the file and supports moving across drives:
Move-item
-Path
E: \ pstest \ newdir \ new.txt
-Destination
D :\
-Passthru
-Force
3.2 move directory:
Move-item
-Path
E: \ pstest \ newdir
-Destination
E: \ A \ newdir
-Passthru
-Force
Move a directory and overwrite it if it exists. Note that the source directory and target directory cannot be located in the same directory and cannot be moved across drives:
Move-item
-Path
E: \ pstest \ newdir
-Destination
D :\
-Passthru
-Force
Error:
Move-item: Source and Destination path must have identical roots. move will not work against SS volumes
.
At line: 1 CHAR: 10
+ Move-item <-path E: \ pstest \ newdir-destination D: \-passthru-Force
+ Categoryinfo: writeerror: (E: \ pstest \ newdir: directoryinfo) [move-item], ioexception
+ Fullyqualifiederrorid: movedirectoryitemioerror, Microsoft. powershell. commands. moveitemcommand
If you need to move the directory across drives, you can copy the directory before deleting it.
Note:
1,Move-itemFiles can be moved across drives, but directories cannot.
2,Copy-itemFiles and directories can be copied across drives.
4. Remove-item: delete files and directories.
4.1 delete an object:
Remove-item
-Path
E: \ pstest \ newdir \ new.txt
-Recurse
4.2 Delete A directory:
Remove-item
-Path
E: \ pstest \ newdir
If the directory contains files, the following message is displayed:
If you do not want to add the-recurse parameter, add-confirm to the prompt.
5. Rename-item: Rename the file and directory.
5.1. rename a file:
Rename-item
-Path
E: \ pstest \ newdir \ new.txt
Old.txt
-Passthru
5.2. Rename the directory:
Rename-item
-Path
E: \ pstest \ newdir
Olddir
-Passthru
6,Invoke-item: open the file and directory.
6.1 open a file:
Invoke-item
C: \ windows \ win. ini
Windows calls the default value of the specified file.ProgramTo open the file, just like double-click to open the file:
6.2 open the directory:
Invoke-item
C: \ WINDOWS
Similar to opening a file, call windows Resource Manager.
Summary:
this time, we learned the basic commands for powershell file system operations and used common parameters, for the usage of other parameters, see the document. Powershell commands are quite flexible. Many parameters can be freely combined and may be in an indefinite order, which provides great convenience for administrators. The file system is the basic part of Computer Management.