powershell - 使用带步骤的范围运算符

标签 powershell powershell-2.0 higher-order-functions array-map

PowerShell 范围运算符生成值列表:

>1..6

1
2
3
4
5
6

如何生成具有特定步骤的值列表?例如,我需要一个从 1 到 10 的列表,其中包含第 2 步。

最佳答案

范围运算符本身不支持跳过/步进,但您可以使用 Where-Object (或 Where() 方法,如果您运行的是 4.0 或更高版本)来过滤每秒:

PS C:\> (1..10).Where({$_ % 2 -eq 0})
2
4
6
8
10

2.0 及以上版本:
PS C:\> 1..10 |Where-Object {$_ % 2 -eq 0}
2
4
6
8
10

关于powershell - 使用带步骤的范围运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892174/

相关文章:

powershell - 使Powershell脚本仅获取为该过滤器定义的前10000个文件

powershell - PowerShell Set-ItemProperty与点设置属性

rust - 为什么在向量上调用过滤器不会从向量中删除元素?

python - 通过Python中的元素总和查找列表列表的最大值

powershell - PowerShell脚本开始在100多行输出之后添加空白行吗?

面向开发人员的 PowerShell

windows - 带有小时和分钟的 robocopy MAXAGE/MINAGE 值

powershell - “How to Read an SDK”是否为PowerShell更新?

typescript - 为 TypeScript 中的重载捕获传递函数的泛型类型

powershell - 将base64字符串转换为X509证书