我在PowerShell上使用以下命令来创建特定目录中所有文件和子文件夹的列表:
get-childitem -path c:\users\username\desktop\test -recurse | select name
因此,假设我的桌面上有一个名为"test“的文件夹,并且在这个文件夹中有三个文件和一个子文件夹,这个子文件夹本身包含更多的文件和子文件夹等等,我确实得到了类似以下内容的输出:
subfolder 1 of "test"
file 1 in "test"
file 2 in "test"
file 3 in "test"
subfolder a of "subfolder 1"
file 1 in subfolder 1
file 2 in subfolder 1
file 3 in subfolder 1
file 1 in subfolder a
file 2 in subfolder a
file 3 in subfolder a
这很好,但我想得到另一种输出,看起来像这样:
+ c:\users\username\desktop\test
| - file 1 in "test"
| - file 2 in "test"
| - file 3 in "test"
|--+ subfolder 1 of "test"
| | - file 1 in subfolder 1
| | - file 2 in subfolder 1
| | - file 3 in subfolder 1
| |--+ subfolder a of "subfolder 1"
| | | - file 1 in subfolder a
| | | - file 2 in subfolder a
| | | - file 3 in subfolder a
|--+ subfolder 2 of "test"
| | -
| | .
| . .
. .
.
这是可能的吗(如果是-那是如何实现的呢?)来得到这样的输出?
我知道当时有一个叫“树”的dos命令,但由于它的局限性,它不能与PowerShell中的get-子项的输出一起工作。在PowerShell中有没有某种等效的命令,或者我可以使用get-childitem命令和它的开关/ additions / ...
对不起,我的英语不好。还有:对不起,我在PowerShell上完全是初学者。
发布于 2020-10-08 02:39:31
你在cmd中习惯的“树”是system32文件夹中的一个应用程序,而不是一些硬编码的cmd功能。
因此,您仍然可以像往常一样从powershell运行它。
例如:
Tree 'C:\Foldername'
Robocopy和其他一些著名的应用程序也是以同样的方式工作的。
外部程序中的错误可以在$LastExitCode而不是通常的$Error中捕获。这些代码的含义根据程序的不同而有所不同。
发布于 2020-10-08 13:04:35
您可以从Powershell调用任何cmd/DOS可执行文件。只要你这样做得当。在控制台主机(powershell.exe/pwsh.exe)中,这实际上与使用cmd.exe相同,但在ISE中则略有不同。YOu不能在ISE中使用交互式命令。您可以使用该命令,但您必须传递它所需的所有参数。
在PowerShell控制台主机(powershell.exe/pwsh.exe)中,只需输入...
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.1}
BuildVersion 10.0.19041.1
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
tree |more
Folder PATH listing for volume Data
Volume serial number is CE3D-F392
D:.
├───.vs
│ └───Scripts
│ └───v16
├───.vscode
...
PowerShell: Running Executables
没有理由从头开始。提供此功能的示例甚至模块有很多。
快速搜索会显示您从调整开始或按原样使用...
PowerTip: View Directory List as Tree by Using PowerShell使用PowerShell社区扩展项目中的Show-Tree cmdlet:
Find-Module -Name pscx |
Format-Table -AutoSize
# Results
<#
Version Name Repository Description
------- ---- ---------- -----------
3.3.2 Pscx PSGallery PowerShell Community Extensions (PSCX) base module which implements a general-purpose set of Cmdlets.
#>
Show-Tree e:\data –depth 2
https://serverfault.com/questions/744660/powershell-populating-treeview-with-directory-hierarchy
$objDriveLetters = GET-WMIOBJECT –query "SELECT * from win32_logicaldisk"
$form = New-Object System.Windows.Forms.Form
$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true
foreach ($iDrive in $objDriveLetters)
{
$DriveRoot = Get-Item $iDrive.DeviceID
#$FolderRoot = Get-ChildItem -Path $iDrive.DeviceID
$FolderRoot = Get-Item -Path $iDrive.DeviceID
$treeView.Nodes.Add($FolderRoot.FullName, $FolderRoot.FullName)
}
$form.Controls.Add($treeView)
$form.ShowDialog()
使用PowerShell创建文件系统大小树视图:
https://key2consulting.com/powershell-file-directory-tree-view
#Variables that need to be set for each run
$startFolder = "C:\Program Files"; #The starting folder to analyze
$sourceHTMLFile = "C:\finalTemplate.html"; #The html source template file
$destinationHTMLFile = "C:\final.html"; #The final html file that will be produced, #does not need to exist
$htmlLines = @();
#Function that creates a folder detail record
function CreateFolderDetailRecord
{
param([string]$FolderPath)
#Get the total size of the folder by recursively summing its children
$subFolderItems = Get-ChildItem $FolderPath -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$folderSizeRaw = 0;
$folderSize = 0;
$units = "";
#Account for no children
if($subFolderItems.sum -gt 0)
{
$folderSizeRaw = $subFolderItems.sum;
}
#Determine units for a more friendly output
if(($subFolderItems.sum / 1GB) -ge 1)
{
$units = "GB"
$folderSize = [math]::Round(($subFolderItems.sum / 1GB),2)
}
else
{
if(($subFolderItems.sum / 1MB) -ge 1)
{
$units = "MB"
$folderSize = [math]::Round(($subFolderItems.sum / 1MB),2)
}
else
{
$units = "KB"
$folderSize = [math]::Round(($subFolderItems.sum / 1KB),2)
}
}
#Create an object with the given properties
$newFolderRecord = New-Object –TypeName PSObject
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderPath –Value $FolderPath;
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeRaw –Value $folderSizeRaw
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeInUnits –Value $folderSize;
$newFolderRecord | Add-Member –MemberType NoteProperty –Name Units –Value $units;
return $newFolderRecord;
}
#Function that recursively creates the html for the output, given a starting location
function GetAllFolderDetails
{
param([string]$FolderPath)
$recursiveHTML = @();
#Get properties used for processing
$folderItem = Get-Item -Path $FolderPath
$folderDetails = CreateFolderDetailRecord -FolderPath $FolderPath
$subFolders = Get-ChildItem $FolderPath | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
#If has subfolders, create hmtl drilldown.
if($subFolders.Count -gt 0)
{
$recursiveHTML += "<li><span class='caret'>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)" + "</span>"
$recursiveHTML += "<ul class='nested'>"
}
else
{
$recursiveHTML += "<li>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)";
}
#Recursively call this function for all subfolders
foreach($subFolder in $subFolders)
{
$recursiveHTML += GetAllFolderDetails -FolderPath $subFolder.FullName;
}
#Close up all tags
if($subFolders.Count -gt 0)
{
$recursiveHTML += "</ul>";
}
$recursiveHTML += "</li>";
return $recursiveHTML
}
#Processing Starts Here
#Opening html
$htmlLines += "<ul id='myUL'>"
#This function call will return all of the recursive html for the startign folder and below
$htmlLines += GetAllFolderDetails -FolderPath $startFolder
#Closing html
$htmlLines += "</ul>"
#Get the html template, replace the template with generated code and write to the final html file
$sourceHTML = Get-Content -Path $sourceHTMLFile;
$destinationHTML = $sourceHTML.Replace("[FinalHTML]", $htmlLines);
$destinationHTML | Set-Content $destinationHTMLFile
https://stackoverflow.com/questions/64250037
复制相似问题