Get the App
SLTechnology News&Howtos  ›  Servers  › 

Powershell- queries the current file directory hierarchy

Shulou Source: shulou.com Published: 2022-06-02 22:40:01 09月18日 Update

In our daily work, we often need to export the current shared environment or disk file directory hierarchy, etc. At the earliest, when there are few directories, we can clearly see the directory and file hierarchy by using the tree command under CMD, so how do we intuitively display or export a file directory or drive directory hierarchy through powershell?

View directory and file structure under DOS:

Tree /?

Graphically displays the folder structure of the drive or path.

TREE [drive:] [path] [/ F] [/ A]

/ F displays the name of the file in each folder.

/ A uses ASCII characters instead of extended characters.

Powershell to view directories and file structures:

In fact, we can also use the powershell command with the tree command. The simple operation is as follows:

Get-ChildItem D:\ SW_Office_Plus | tree / f

Get-ChildItem D:\ SW_Office_Plus | tree / A

Get-ChildItem: gets items and subitems in one or more specified locations.

Get the folder name under the current directory:

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

Get the file name under the current directory:

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

Next, let's move on to today's topic, how to view the file level under the current directory. The specific commands are as follows:

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

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

If you need to export the results, you can do so with the following command:

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

PS. Add: export file, file directory name, creation time, format and other information:

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

The format after export is as follows, which can be filtered by yourself. For more information on the content of the script, please refer to the link.

Welcome to the official account of Wechat: Xiao Wen study Society.

Tags: File directory structure command hierarchy name folder content character format intuitive next subject location information public graphics multiple subitems situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Microsoft vpn Apple