简体   繁体   English

如何在Powershell命令中设置变量

[英]How to set a variable in a powershell command

I am trying to declare and set a variable in a powershell command. 我正在尝试在powershell命令中声明并设置变量。 Is that possible? 那可能吗?

I was hoping to do something like this: 我希望做这样的事情:

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

Is this possible or can this only be done in a .ps script? 这可能还是只能在.ps脚本中完成?

It can be done easily by just hitting enter in the console after declaring your variable: 只需在声明变量后在控制台中按Enter,即可轻松完成此操作:

$name = "I219" # now hit enter

To access the variable, type it in the console and hit enter again: 要访问该变量,请在控制台中将其键入,然后再次按Enter键:

$name # hit enter => returns I219

Now use it with your command: 现在将其与您的命令一起使用:

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

Or as a one-liner: 还是单线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM