PowerShell manages files and folders
(1) Enquiry
Use Get-ChildItem to get all the items in a folder directly. Add an optional Force parameter to show hidden or system items. In order to display the included items, you also need to specify the-Recurse parameter. (this may take quite a long time to complete. )
Get-ChildItem-Path C:\-ForceGet-ChildItem-Path C:\-Force-Recurse
Get-ChildItem can use its Path, Filter, Include, and Exclude parameters to filter items, but those are usually based only on names. You can also perform complex filtering by using other Where-Object-based properties of the item.
Get-ChildItem-Path $env:ProgramFiles-Recurse-Include * .exe | Where-Object-FilterScript {($_ .LastWriteTime-gt '2005-10-01')-and ($_ .Length-ge 1mb)-and ($_ .Length-le 10mb)}
You can also enumerate using wildcard matching, and Windows PowerShell wildcard representations include:
The asterisk (*) matches zero or more of any characters that appear. Question mark (?) Exactly matches one character. The left parenthesis ([) character and the right bracket (]) character enclose a set of characters to match.
To find all files in the Windows directory with the suffix .log and exactly five characters in the base name, enter the following command:
Get-ChildItem-Path C:\ Windows\? .log
To find all files that begin with the letter x in the Windows directory, type:
Get-ChildItem-Path C:\ Windows\ x *
To find all files whose names begin with x or z, type:
Get-ChildItem-Path C:\ Windows\ [xz] *
You can also use exclusion parameters, such as excluding files whose names contain 9516:
Get-ChildItem-Path C:\ WINDOWS\ System32\ w*32*.dll-Exclude * [9516] * Get-ChildItem-Path C:\ Windows\ * .dll-Recurse-Exclude [Amury] * .dll
Include and exclude parameters can be used in combination:
Get-ChildItem-Path C:\ Windows-Include * .dll-Recurse-Exclude [Amury] * .dll
(2) replication
If the destination file or folder already exists, the replication attempt fails. To override a pre-existing target, use the Force parameter. This command is valid even when the target is read-only.
Copy-Item-Path C:\ boot.ini-Destination C:\ boot.bak-Force
You can still use other tools to perform file system replication. XCOPY, ROBOCOPY, and COM objects, such as Scripting.FileSystemObject, are all applicable to Windows PowerShell. For example, you can use the Windows Script Host Scripting.FileSystem COM class to back up C:\ boot.ini to C:\ boot.bak:
(New-Object-ComObject Scripting.FileSystemObject) .CopyFile ('C:\ boot.ini','C:\ boot.bak')
(3) create
If a Windows PowerShell provider has multiple types of items (for example, the FileSystem Windows PowerShell provider used to distinguish between directories and files), you need to specify the item type.
New-Item-Path'C:\ temp\ New Folder'-ItemType DirectoryNew-Item-Path'C:\ temp\ New Folder\ file.txt'-ItemType File
(IV) deletion
When deleting, if you do not want the system to prompt you for each contained item, specify the Recurse parameter:
Remove-Item-Path C:\ temp\ DeleteMe-Recurse
You can also follow the official Wechat account below for more information.