简体   繁体   English

在单个 PowerShell 行中设置变量

[英]Set variable in a single PowerShell line

Can any of you let me know how to state a variable in a PowerShell line?你们中的任何人都可以告诉我如何在 PowerShell 行中 state 变量吗? Just like I could do using PowerShell ISE I would like to be able to do so via the plain console.就像我可以使用 PowerShell ISE 一样,我希望能够通过普通控制台进行操作。

You can assign values to a PowerShell variable by combining a variable name, an assignment operator, and an expression.您可以通过组合变量名称、赋值运算符和表达式为 PowerShell 变量赋值。 Here is a simple example:这是一个简单的例子:

>> $a = 1 + 1

If you want to populate multiple variables with the same value, you can save some typing as in the example below:如果你想用相同的值填充多个变量,你可以保存一些输入,如下例所示:

>> $a = $b = $c = 1

You can also define multiple variables with different values on one line:您还可以在一行上定义多个具有不同值的变量:

>> $a, $b, $c = 1, 2, 3

To display the value of a variable, you don't need a special command as in many other programming languages;要显示变量的值,您不需要像在许多其他编程语言中那样的特殊命令; entering the variable name is enough.输入变量名就足够了。 This works in a script and on a command prompt.这适用于脚本和命令提示符。

>> $c

To take values from user, similar to Python input() function you can use:要从用户那里获取值,类似于 Python input() function 您可以使用:

$Number = Read-Host "Please enter a number" 

Read the official documentation here .此处阅读官方文档。

I finally found the solution to get the job done.我终于找到了完成工作的解决方案。 I just set the variable and end it with a semi-colon.我只是设置变量并以分号结束。 Then I can go on with scripting.然后我可以使用脚本编写 go 。

Thank you all.谢谢你们。

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

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