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

PowerShell Remove all user defined variable in PowerShell的更多相关文章

  1. Powershell Remove "Limited Access" - 金大昊(jindahao)

    对于有多级web获取getlist会报错:Exception calling “GetList” with “1” argument $SPWeb = Get-SPWeb -Identity http ...

  2. Jenkins+PowerShell持续集成环境搭建(四)常用PowerShell命令

    0. 修改执行策略 Jenkins执行PowerShell脚本,需要修改其执行策略.以管理员身份运行PowerShell,执行以下脚本: Set-ExecutionPolicy Unrestricte ...

  3. PowerShell工作流学习-2-工作流运行Powershell命令

    关键点: a)inlineScript 活动具有活动通用参数,但不具有PowerShell 通用参数,且inlineScript 脚本块中的命令和表达式不具有工作流的功能b)默认inlineScrip ...

  4. How to check a not defined variable in javascript

    javascript里怎么检查一个未定义的变量? in JavaScript null is an object. There's another value for things that don' ...

  5. 【原创】NuGet 出现“无法初始化 PowerShell 主机,如果将你的 PowerShell 执行策略设置设置为 AllSigned ,请先打开程序包管理控制台以初始化该主机” 错误的解决方法

    现象: 网上的设置 AllSigned 等方法都无效..后来考虑可能跟命令行版本兼容性有关系,然后在注册表命令行配置里发现一 ForceV2 设置项,抱着试一试的心态改了下,果然解决了! 解决方法:修 ...

  6. PowerShell Notes

    1.  概要 - PowerShell 是cmdlet(command-let)的指令集合,类似unix的bash. - IDE: Windows PowerShell ISE,不区分大小写,可以用命 ...

  7. Powershell对象选择,排序和变量存储

    PowerShell基础教程(17)——对象的选择.排序和变量存储 可以使用 Select-Object cmdlet 来创建新的.自定义的 Windows PowerShell 对象,后者包含的属性 ...

  8. 【189】◀▶ PowerShell 系统学习

    参考网站如下: PowerShell 中文博客      PowerShell 博客——叹为观止 Mater-PowerShell      通过 PowerShell 编写脚本      Power ...

  9. 【黑客基础】Windows PowerShell 脚本学习(上)

    视频地址:[黑客基础]Windows PowerShell 脚本学习 2019.12.05 学习笔记 1.$PSVersionTable :查看PowerShell的版本信息. 2.PowerShel ...

随机推荐

  1. linux重新编译内核

    一.linux内核 1.查看linux内核版本 uname -r 2.下载对应的linux内核 https://www.kernel.org/pub/linux/kernel/ 将内核文件夹解压到/u ...

  2. mac 版本号控制工具SmartSVN7.5.4(破解版)

    SmartSVN7.5.4和破解工具,下载地址:http://download.csdn.net/detail/pearlhuzhu/7407319 操作步骤: 1.在MAC上选中smartsvn-m ...

  3. md笔记——正则学习

    正则表达式 在线调试正则1 在线调试正则2 规则记录 \d 匹配一个数字字符.等价于[0-9] \D 匹配一个非数字字符.等价于[^0-9]. . 通配符,可以匹配任意字符. ? 表示量词" ...

  4. JavaScript引用类型之Array数组的拼接方法-concat()和截取方法-slice()

    1.concat()   基于当前数组中的所有项创建一个新数组(也就是副本),然后将接收到的参数添加到副本的末尾,最后返回新构建的数组.也就是说,concat()在向数组中追加元素时,不会改变原有数组 ...

  5. C#Base64加密

    using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;u ...

  6. python正则表达式实例

    1.将"(332.21)luck李."中(332.21)抽取出来同时能够 将”(23)luck李.“中的(23)抽取出来 pp = re.compile('(\(\d*(.\d*) ...

  7. latex 常用小结

    在写论文,甚至有些课程的报告的时候,latex是常用的工具.这篇博文简单的记录了latex常用的一些内容. 1 基本模块 没用过latex的读者,最想问的问题莫过于latex的 “hello worl ...

  8. python 程序中设置环境变量

    python 中调用系统命令有三种方法: 1.os.system('command') ,这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 ...

  9. spring util命名空间

    在spring的配置文件中util命名空间类似于java.util包类对应,util命名空间提供了集合相关的配置,在使用命名空间前要导入util命名空间,如下: util命名空间引入 <bean ...

  10. Node爬虫

    Node爬虫 参考 http://www.cnblogs.com/edwardstudy/p/4133421.html 所谓的爬虫就是发送请求,并将响应的数据做一些处理 只不过不用浏览器来发送请求 需 ...