How to delete private variable

Himanshuk 161 Reputation points
2021-01-12T15:32:01.203+00:00
Set-variable -name "foo" -value "doo" -visibility private

I am unable to delete value using remove or clear variable

The use case is, on console I am passing argument list for exe in that arguments I am passing if else loop and value inside in it and I want to hide the value on console, the argument list coming from json template contains command too

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,537 questions
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 33,996 Reputation points
    2021-01-12T17:15:52.5+00:00

    and I want to hide the value on console,

    What statement is causing the value to be displayed?

    Can't you just set it to null when you're done referencing it.

    $foo = $null  
    

    Is this a scope problem?

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.1

    $global:foo = $null
    

  2. MotoX80 33,996 Reputation points
    2021-01-13T13:25:03.023+00:00

    Sorry, I have no idea what you are trying to do. What .exe are you running? Are you passing code to this .exe?

    Here is a Powershell script that accepts an input parameter and runs a program with that value. Note that what when I run it in the Powershell window, the $foo variable is empty because it's scope was in the script and it's value is gone because the script has terminated.

    The character string "/user" displays in the console for 2 reasons.

    1. The script intentionally outputs it. The "Calling whoami" statement can be removed which would prevent the display.
    2. It was entered on the command line. Clear-Host will clear the screen.

    56169-capture.jpg

    Note that whoami.exe does not output anything that says "I was called with the /user switch". If the .exe that you are calling displays $foo then you have to capture its output and process it.

    $z =  &c:\Windows\system32\whoami.exe $foo  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.