Powershell 导出目录文件名

Powershell 导出目录文件名,powershell,export,get-childitem,Powershell,Export,Get Childitem,我正在尝试创建一个脚本,该脚本将提示导出到.csv的路径,该路径显示文件夹的名称和文件夹内容的名称。类似这样,但没有模式和长度 我想保持每个文件夹之间的间隙 示例-(不带模式/长度) 电流输出: Please enter the the path of the fold you wish to export: C:\Users\khalifam\Desktop\TestFolder1 DirectoryName FullN

我正在尝试创建一个脚本,该脚本将提示导出到.csv的路径,该路径显示文件夹的名称和文件夹内容的名称。类似这样,但没有模式和长度

我想保持每个文件夹之间的间隙

示例-(不带模式/长度)

电流输出:

Please enter the the path of the fold you wish to export: C:\Users\khalifam\Desktop\TestFolder1 DirectoryName FullName Root ------------- -------- ---- C:\Users\khalifam\Desktop\TestFolder1\TestFolder2 C:\ C:\Users\khalifam\Desktop\TestFolder1\TestFolder3 C:\ C:\Users\khalifam\Desktop\TestFolder1\TestFolder2 C:\Users\khalifam\Desktop\TestFolder1\TestFolder2\EC2Key1.pem C:\Users\khalifam\Desktop\TestFolder1\TestFolder3 C:\Users\khalifam\Desktop\TestFolder1\TestFolder3\Dropbox-CCEN-Course.docx 请输入要导出的折叠路径:C:\Users\khalifam\Desktop\TestFolder1 目录名全名根目录 ------------- -------- ---- C:\Users\khalifam\Desktop\TestFolder1\TestFolder2 C:\ C:\Users\khalifam\Desktop\TestFolder1\TestFolder3 C:\ C:\Users\khalifam\Desktop\TestFolder1\TestFolder2 C:\Users\khalifam\Desktop\TestFolder1\TestFolder2\EC2Key1.pem C:\Users\khalifam\Desktop\TestFolder1\TestFolder3 C:\Users\khalifam\Desktop\TestFolder1\TestFolder3\Dropbox-CCEN-Course.docx
首先递归枚举目录,然后不递归地处理每个目录的内容。请注意,由此生成的输出不是实际的CSV

Get-ChildItem $FilePathLocation -Directory -Recurse | ForEach-Object {
    "{0}`n" -f $_.FullName
    Get-ChildItem $_.FullName |
        Select-Object Name, LastWriteTime |
        Format-Table |
        Out-String
} | Set-Content 'output.txt'
还请注意,这需要PowerShell v3或更高版本。如果您使用的是旧版本,则需要删除参数
-Directory
,并使用
Where Object
过滤器。

您拥有大部分

$FilePathLocation = Read-Host -Prompt 'Please enter the the path of the fold you wish to export'

Get-ChildItem $FilePathLocation -Recurse | select DirectoryName, FullName | Export-Csv C:\PathStuff.csv

如果您想导出到csv,则不能使用格式表,它会将输出弄乱

非常好,谢谢,您能解释一下背后的逻辑吗?
Get-ChildItem $FilePathLocation -Directory -Recurse | ForEach-Object {
    "{0}`n" -f $_.FullName
    Get-ChildItem $_.FullName |
        Select-Object Name, LastWriteTime |
        Format-Table |
        Out-String
} | Set-Content 'output.txt'
$FilePathLocation = Read-Host -Prompt 'Please enter the the path of the fold you wish to export'

Get-ChildItem $FilePathLocation -Recurse | select DirectoryName, FullName | Export-Csv C:\PathStuff.csv