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. 什么是CTS、CLS和CLR

    CTS.CLS和CLR是.NET框架的基本概念,但往往成为程序员们忽视的部分.清楚理解三者的概念,有助于.NET程序员编写高效安全的.NET程序. 所涉及到的知识点 •   .NET的基本概念 •   ...

  2. javaScript 工作必知(二) null 和undefined

    null null 表示个“空” , 使用typeof (null) ;//Object ; 说明他是一个特殊的对象. null 类型只自己唯一个成员.他是不包含属性和方法的. undefined u ...

  3. HTML系列(六):划分文档结构

    常见的网页结构布局是酱紫的,真是美美哒^O^: 一.添加基本标题h1~h6(没什么好说的): 二.标题分组hgroup <hgroup>用来将标题和子标题进行分组.如果一篇文章articl ...

  4. 基于Jquery的Ajax分页,只有上一页和下一页

    最近项目中用到ajax分页 在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接 http://www.zhangxinxu.com/wordpress/2010/ ...

  5. C++的标准模板库(STL)简介

    STL(Standard Template Library,标准模板库)是C++对泛型编程思想的实现,最早是惠普实验室开发的.在被引入C++之前该技术就已经存在了很长的一段时间.后来STL成为ANSI ...

  6. NSURLSessionConfiguration的简单实用

    NSURLSessionConfiguration 基于前面学习了NSURLSession的知识,这边文章就讲下NSURLSessionConfiguration相关应用,(这名字可真长). 简而言之 ...

  7. 如何为你的美术妹子做Unity的小工具(一)

    在上的工具栏添加   也就是这个位置

  8. 为什么IIS中找不到.net framework 4.5(转)

    .net 4.5是4.0的update,所以直接用4.0部署就可以了 .NET 4.5 is an in-place replacement for .NET 4.0, When .NET 4.5 i ...

  9. hdu 4031 Attack 线段树

    题目链接 Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total ...

  10. Linux内核和驱动编译常见问题

    1.获取内核源码应为 git clone depth=1  不然编译时会报错 2.有些RTL188CUS网卡与wpa_supplicant兼容有问题 更新wpasupplicant或许能解决 这里用i ...