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