首页 > 解决方案 > 如何在powershell命令中设置变量

问题描述

我正在尝试在 powershell 命令中声明和设置一个变量。那可能吗?

我希望做这样的事情:

"$name" = "I219" | Get-NetAdapter | where Name -Match "$name"

这是可能的还是只能在 .ps 脚本中完成?

标签: windowspowershell

解决方案


声明变量后,只需在控制台中按 enter 即可轻松完成:

$name = "I219" # now hit enter

要访问该变量,请在控制台中键入它并再次按 Enter:

$name # hit enter => returns I219

现在将它与您的命令一起使用:

Get-NetAdapter | where { $_.Name -Match $name }

或作为单行:

$name = "I219"; Get-NetAdapter | where { $_.Name -Match $name }

推荐阅读