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. WPF datagrid 初学

    <Window x:Class="WpfDemo.WinDataGrid" xmlns="http://schemas.microsoft.com/winfx/20 ...

  2. JavaScript引用类型之Array数组的排序方法

    数组中已经存在两个JavaScript给我们定义好的重排序的方法:reverse()和sort()方法,下面来简单分析下: 1.reverse()    用于反转数组项的顺序,代码如下: <sc ...

  3. GridView边线Border设置

    1.黑色实线:(行列都有) <asp:GridViewID="GridView1"runat="server"CellPadding="3&qu ...

  4. UnicodeEncodeError: 'latin-1' codec can't encode character 解决sae flask 中文问题

    #encoding=utf-8 #中文编码支持 import MySQLdb from flask import Flask, g, request app = Flask(__name__) app ...

  5. 5分种让你了解javascript异步编程的前世今生,从onclick到await/async

      javascript与异步编程 为了避免资源管理等复杂性的问题,javascript被设计为单线程的语言,即使有了html5 worker,也不能直接访问dom. javascript 设计之初是 ...

  6. PHPEXCEL导入小技巧

    在导入excel的时候,单元格格式和公式经常让导入不顺畅.注意phpexcel文档说明,基本上就可以很顺利的导入. 1.忽略单元格格格式,并导入xls.xlsx两种格式 $objReader = PH ...

  7. Ie浏览器TextBox文本未居中

    Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中  vertical-align:middle

  8. Common Git command and mean (Windows)

    Config: git config --system git config --global git config --global merge.tool vimdiff Check config: ...

  9. ECC(Error Checking and Correction)校验和纠错

    ECC的全称是 Error Checking and Correction or Error correction Coding,是一种用于差错检测和修正的算法.上一节的BBM中我们提到过,NAND闪 ...

  10. 【Delphi内联汇编学习1】Delphi与汇编

    我一直认为Delphi功能与C++相比毫不逊色,提供了丰富的控件和类.全部API以及嵌入的汇编.最近小弟在把C版的Huffman压缩改用Delphi写时,顺便“研究”了一下Delphi的位操作和嵌入式 ...