• PowerShell Remove all user defined variable in PowerShell


    When PS scripts executes, it is possibly create much user defined variables.

    So, sometimes these varibales may confuse us a lot.

    Here's a workaound:

    Most of the standard variables can be found in System.Management.Automation.SpecialVariables. If you filter out these and a small list of other known variables, you can create a reusable function to get user-defined variables:

    function Get-UDVariable {
      get-variable | where-object {(@(
        "FormatEnumerationLimit",
        "MaximumAliasCount",
        "MaximumDriveCount",
        "MaximumErrorCount",
        "MaximumFunctionCount",
        "MaximumVariableCount",
        "PGHome",
        "PGSE",
        "PGUICulture",
        "PGVersionTable",
        "PROFILE",
        "PSSessionOption"
        ) -notcontains $_.name) -and `
        (([psobject].Assembly.GetType('System.Management.Automation.SpecialVariables').GetFields('NonPublic,Static') | Where-Object FieldType -eq ([string]) | ForEach-Object GetValue $null)) -notcontains $_.name
        }
    }

    in your script, just use :

    Get-UDVariable | Remove-Variable

    One more suggestion:

    I don't think it will be the best solution to solve problems made by remaining varibales.

    When we create varibale in our scripts, we should have a think that what is the proper scope ?

    MSDN ref : https://technet.microsoft.com/en-us/library/hh847849.aspx

  • 相关阅读:
    最大流基础
    转一篇写期望的日志
    poj3267The Cow Lexicon(dp)
    poj3026Borg Maze(bfs+MST)
    poj2226Muddy Fields(二分图的最小点覆盖)
    poj1035Spell checker(字符串模拟)
    poj1422Air Raid(最小路径覆盖)
    poj3020Antenna Placement(最小路径覆盖)
    [JSOI2009]火星藏宝图
    [SHOI2007]书柜的尺寸
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/5445873.html
Copyright © 2020-2023  润新知