【发布时间】:2026-02-13 18:15:01
【问题描述】:
我希望创建一个脚本传递给用户,该用户询问文件路径位置并将其中包含的所有文件名导出为 CSV,目录必须是文件夹。
# set the variable $FilePathLocation to whatever the user specifies
$FilePathLocation = Read-Host -Prompt 'Please enter the path location you wish to export'
# set the working directory
Set-Location $FilePathLocation
# select the names of all the content in the folder and pop it in to a CSV file within the location
dir -Recurse | Select Name | ConvertTo-Csv | Out-File "test.csv"
提示:
请输入您要导出的路径位置:“C:\Users\khalifam\Desktop\Depositions 进入试用包”错误:
设置位置:找不到驱动器。名为“C”的驱动器不存在。 在行:3 字符:1 + 设置位置 $FilePathLocation + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ("C:String) [Set-Location], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand【问题讨论】:
-
您在路径中使用了
"还是'?试试吧,只要 C:\bla\bla -
您的计算机没有驱动器
"C:(请注意错误消息中的前导双引号)。输入不带双引号的路径,或在输入路径后去掉前导/尾随双引号。 -
哎呀,我的错,它是“,我在没有语音标记的情况下尝试了它,效果很好。谢谢大家
-
& 还有我如何进行导出显示或在子文件夹中显示某种内容指示,因为它目前只是一个平面项目列表@AnsgarWiechers
-
Select Name->Select FullName。另外,请使用Export-Csv而不是ConvertTo-Csv | Out-File。Get-ChildItem $FilePathLocation -Recurse | Select-Object FullName | Export-Csv 'test.csv' -NoType
标签: powershell csv