mjy0424
mjy0424

Reputation: 11

Powershell variable is lost

My code:

    Write-Output "mytest" 
    $var1="defucntion"  
    $var1  
    Get-ChildItem variable:var*  
    Remove-Variable $var1  
    Get-ChildItem Variable:

I entered the code into Visual Studio Code. But got an error like this:

Remove-Variable : 找不到名为“defucntion”的变量。
所在位置 C:\Users\Administrator\mine\Ps1 1\test1.ps1:5 字符: 1
+ Remove-Variable $var1
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (defucntion:String) 
[RemoveVariable], ItemNotFoundException
+ FullyQualifiedErrorId : 
VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand

Upvotes: 0

Views: 177

Answers (1)

andr3yk
andr3yk

Reputation: 176

You need to be passing through name reference as a string to Remove-Variable

Remove-Variable "var1"

SYNTAX
    Remove-Variable [-Name] <string[]>

In your case you are passing though a variable value, e.g. "defucntion", but variable with name "defucntion" doesn't exist.

p.s. You can check help for functions by using Get-Help to identify parameter types

Upvotes: 2

Related Questions