Powershell-查询当前文件目录层级结构

平常工做中咱们每每有须要导出当前共享环境或磁盘文件目录层级结构等的需求,最先在目录少的状况下咱们使用CMD下tree 命令能够很清晰的看到目录、文件层级结构,那么咱们又如何经过powershell直观显示或导出某文件目录或盘符目录层级结构呢?html

DOS下查看目录、文件结构:shell

tree /?
以图形显示驱动器或路径的文件夹结构。
TREE [drive:][path] [/F] [/A]
/F   显示每一个文件夹中文件的名称。
/A   使用 ASCII 字符,而不使用扩展字符。微信

image


Powershell查看目录、文件结构:ide

其实咱们经过powershell命令也能够搭配tree命令使用,简单操做以下:ui

Get-ChildItem D:\SW_Office_Plus |tree /f
Get-ChildItem D:\SW_Office_Plus |tree /Aspa

Get-ChildItem :获取一个或多个指定位置中的项和子项。code

获取当前目录下文件夹名称:orm

Get-ChildItem D:\SW_Office_Plus | ?{$_.psiscontainer -eq $true}htm

获取当前目录下文件名称:
Get-ChildItem D:\SW_Office_Plus | ?{$_.psiscontainer -eq $false}blog

image

接下来进入咱们今天的主题内容,如何查看当前目录下文件层级,具体命令以下:

Get-ChildItem -Recurse -Directory -Depth 3 |select FullName

Get-ChildItem D:\SW_Office_Plus  -Recurse -Directory -Depth 3 |select Fullname

image

若是须要对结果进行导出,可经过以下命令操做:

Get-ChildItem -Recurse -Directory -Depth 3 |select FullName | Export-Csv d:\fullname.csv -Encoding UTF8 –NoTypeInformation



PS.补充:导出文件、文件目录名称、建立时间、格式等等信息:

Get-ChildItem -Path D:\SW_Office_Plus -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[@($ParentS.Length - 2)]
$ParentPath = $_.PSParentPath
$ParentPathSplit = ($_.PSParentPath).split("::")
$ParentPathFinal = $ParentPathSplit[@($ParentPathSplit.Length -1)]
#$ParentPath = [io.path]::GetDirectoryName($myDirectory)
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
    @{n="Name";e={$Item}},`
    @{n="Created";e={$Age}},`
    @{n="Folder Name";e={if($Parent){$Parent}else{$Parent}}},`
    @{n="filePath";e={$Path}},`
    @{n="Extension";e={if($Folder){"Folder"}else{$Type}}},`
    @{n="Folder Name 2";e={if($Parent){$Parent}else{$Parent}}},`
    #@{n="Folder Path";e={$ParentPath}},`
    @{n="Folder Path 2";e={$ParentPathFinal}}`
}| Export-Csv d:\Folder.csv -Encoding UTF8 -NoTypeInformation

image

导出后格式以下,可自行筛选,该脚本内容具体可参考该link

image

欢迎关注微信公众号:小温研习社

欢迎关注yanxishe.png

相关文章
相关标签/搜索