In PowerShell, which code snippet deletes the variable MyVar from the current PowerShell session?

Hi

In PowerShell, which code snippet deletes the variable MyVar from the current PowerShell session?

Assume the current working directory is C:\Temp on the local file system, and MyVar contains an integer value.

5 Likes

You can use the ‘Remove-Variable’ cmdlet to delete a variable from the current PowerShell session. Here’s the code snippet you can use:

Remove-Variable -Name MyVar -Force

Explanation:

  • Remove-Variable is a cmdlet used to delete variables in PowerShell.
  • -Name MyVar specifies the name of the variable you want to delete, in this case, “MyVar”.
  • -Force is an optional parameter that forces the removal of the variable without asking for confirmation.

The variable “MyVar” will be deleted from the current session by running this code snippet in PowerShell.

4 Likes