Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PowerShell manages files and folders

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

(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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report