I want to remove all user-created variables at the start of a script.
Currently I am doing Remove-Variable -Name * but it tries to dele
Instead of deleting all the user variables, start a fresh instance of PowerShell:
PS C:\> $x = 10
PS C:\> $y = 50
PS C:\> $blah = 'text'
PS C:\> Write-host $x $y $blah
10 50 text
PS C:\> powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\> Write-host $x $y $blah
PS C:\>
User defined variables won't carry over into the new instance.
PS C:\> $bleh = 'blue'
PS C:\> Write-Host $bleh
blue
PS C:\> exit
PS C:\> Write-host $bleh
PS C:\>
Your variables won't carry back over into the calling instance, either.
You have a few options in terms of how to actually accomplish this.
You can always start the new instance yourself, of course:
powershell -ExecutionPolicy Unrestricted -File myscript