powershell - 将目录中的文件列表添加到数组列表

标签 powershell get-childitem

我正在尝试将文件列表保存在目录到数组列表中,能否请您共享Windows PowerShell的代码段。

我在c:\ temp下有2个文件

文件1:stack.zip
文件2:overflow.zip

需要将file1和file2存储在名为的数组中

$arrlst = ['stack.zip','overflow.zip']
Set-Location -Path "c:\temp"
fileslst = Get-children
$arrlst = [filelist]

最佳答案

运行以下内容将为您提供服务。

[System.Collections.ArrayList]$arrlst = @(
    $(Get-ChildItem -File -Path 'C:\temp' | Select -ExpandProperty Name)
)

您需要执行Select -ExpandProperty Name以确保Get-ChildItem的唯一结果是包含扩展名的文件名(Name)。

关于powershell - 将目录中的文件列表添加到数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58297648/

相关文章:

azure - 在azure中禁用子网之间的通信

powershell - 为什么这个 powershell 脚本会发出两个 Web 请求 (System.Net.HttpWebRequest)?

powershell - 在 PowerShell 中,如何获取具有特定属性的所有注册表项?

Powershell:为什么 (gci c:\ddd).count on an Empty 文件夹不返回 0

powershell - 基于命令行的ProcessID如何匹配

powershell - 将逗号分隔的字符串转换为列表

powershell - 如何在 powershell 中将字符串转换为 24 小时格式的日期?

powershell - "["和"]"字符弄乱了get-childitem

arrays - 将两个不同的Get-ChildItem调用的结果合并为一个变量,以对它们进行相同的处理

powershell - 在目录中搜索特定的文件类型,如果PowerShell中存在该文件,则返回true或false