当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell -OutVariable 参数

作者:精品下载站 日期:2024-12-14 17:02:03 浏览:12 分类:玩电脑

PowerShell -OutVariable 参数


PowerShell -OutVariable 是一个有用的参数,它将命令的输出存储在指定的变量中,同时还将输出发送到控制台。使用 outvariable 可以存储命令输出、操作和数据处理。

使用-OutVariable参数,它将命令输出存储在变量中,要在同一变量中添加另一个命令输出,请在变量名称前使用加号(+)。

OutVariable 创建的变量的类型为[System.Collections.ArrayList]。使用 PowerShell GetType() cmdlet 检查变量的类型。

在本文中,我们将讨论如何使用 PowerShell -OutVariable 参数来存储命令输出。

PowerShell-输出变量

除了将输出发送到控制台或管道之外,还可以使用 -OutVariable 参数将命令的输出存储在指定变量中。

Get-ChildItem -Path D:\Logs\ -OutVariable logFiles

在上面的 PowerShell 脚本中,Get-ChildItem cmdlet 使用 Path 变量从指定位置获取项目。 PowerShell -OutVariableGet-ChildItem 命令输出存储在 logFiles 变量中,并将输出发送到控制台。

上述 PowerShell 脚本的输出使用 -OutVariable 参数将输出存储在 logFiles 中,并在控制台上显示输出,如下所示:

[玩转系统] PowerShell -OutVariable 参数

使用存储命令输出的 -OutputVariable 参数在控制台上打印输出。

$logFiles

在上面的 PowerShell 脚本中,$logFiles 变量显示 $logFiles 的内容。

[玩转系统] PowerShell -OutVariable 参数

要将另一个命令的输出添加到 OutVariable 变量,请在变量前键入加号 (+)。它将添加输出而不替换现有输出。

# Use plus sign before the variable to add the output

Get-Content -Path D:\Logs\Log3.txt -OutVariable +logFiles

# Print the output of the variable
$logFiles

在上面的 PowerShell 脚本中,Get-Content cmdlet 读取文件内容并在 PowerShell 终端上显示输出。 OutVariable 参数将输出添加到 logFiles 变量中。

上述 PowerShell 脚本的输出是:

[玩转系统] PowerShell -OutVariable 参数

结论

希望上面关于如何使用 PowerShell -OutVariable 参数除了在控制台上显示输出之外存储命令的输出的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯