Windows Powershell 定义变量

2019-09-30 15:46:36丽君

但是可以通过删除变量,再重新创建变量更新变量内容。

PS C:test> del Variable:num -Force
PS C:test> $num=101
PS C:test> $num
101

有没有权限更高的变量,有,那就是:选项Constant,常量一旦声明,不可修改

PS C:test> new-variable num -Value "strong" -Option constant

PS C:test> $num="why? can not delete it."
Cannot overwrite variable num because it is read-only or constant.
At line:1 char:5
+ $num <<<< ="why? can not delete it."   + CategoryInfo     : WriteError: (num:String) [], SessionStateUnauthorizedAccessException   + FullyQualifiedErrorId : VariableNotWritable PS C:test> del Variable:num -Force
Remove-Item : Cannot remove variable num because it is constant or read-only. If the variable is read-only,
ration again specifying the Force option.
At line:1 char:4
+ del <<<< Variable:num -Force
  + CategoryInfo     : WriteError: (num:String) [Remove-Item], SessionStateUnauthorizedAccessExcepti
  + FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveItemCommand

变量描述
在New-Variable 可以通过-description 添加变量描述,但是变量描述默认不会显示,可以通过Format-List 查看。

PS C:test> new-variable name -Value "me" -Description "This is my name"
PS C:test> ls Variable:name | fl *

PSPath    : Microsoft.PowerShell.CoreVariable::name
PSDrive    : Variable
PSProvider  : Microsoft.PowerShell.CoreVariable
PSIsContainer : False
Name     : name
Description  : This is my name
Value     : me
Visibility  : Public
Module    :
ModuleName  :
Options    : None
Attributes  : {}