PowerShell ways to traverse files, folders _powershell

Source: Internet
Author: User

PowerShell it is easy to traverse subfolders and files under a folder. Get-childitem This cmdlet has a recurse parameter that is used to traverse a folder.

PowerShell, use Get-childitem to get the subfolders and files underneath the folder (of course, it does not just do this). We can then use the Foreach-object cmdlet to iterate through the following child objects. The Psiscontainer property is then used to determine whether it is a folder or a file.

Get-childitem, gets the collection of all child objects for the specified object.
Example:

Copy Code code as follows:

#获取D: \ object, return value type System.IO.DirectoryInfo
Get-childitem D:\

#输出D: \ file name of all files below
Get-childitem D:\ | foreach-object-process{
if ($_-is [System.IO.FileInfo])
{
Write-host ($_.name);
}
}

#列出今天创建的文件
Get-childitem D:\ | foreach-object-process{
if ($_-is [System.IO.FileInfo]-and ($_. Creationtime-ge [System.datetime]::today)]
{
Write-host ($_.name,$_. CreationTime);
}
}

#找出D盘根目录下的所有文件
Get-childitem D:\ | ? {$_.psiscontainer-eq $false}


If you are looking for a folder, replace $false with $true

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.