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
PowerShell Remove all user defined variable in PowerShell的更多相关文章
- Powershell Remove "Limited Access" - 金大昊(jindahao)
对于有多级web获取getlist会报错:Exception calling “GetList” with “1” argument $SPWeb = Get-SPWeb -Identity http ...
- Jenkins+PowerShell持续集成环境搭建(四)常用PowerShell命令
0. 修改执行策略 Jenkins执行PowerShell脚本,需要修改其执行策略.以管理员身份运行PowerShell,执行以下脚本: Set-ExecutionPolicy Unrestricte ...
- PowerShell工作流学习-2-工作流运行Powershell命令
关键点: a)inlineScript 活动具有活动通用参数,但不具有PowerShell 通用参数,且inlineScript 脚本块中的命令和表达式不具有工作流的功能b)默认inlineScrip ...
- How to check a not defined variable in javascript
javascript里怎么检查一个未定义的变量? in JavaScript null is an object. There's another value for things that don' ...
- 【原创】NuGet 出现“无法初始化 PowerShell 主机,如果将你的 PowerShell 执行策略设置设置为 AllSigned ,请先打开程序包管理控制台以初始化该主机” 错误的解决方法
现象: 网上的设置 AllSigned 等方法都无效..后来考虑可能跟命令行版本兼容性有关系,然后在注册表命令行配置里发现一 ForceV2 设置项,抱着试一试的心态改了下,果然解决了! 解决方法:修 ...
- PowerShell Notes
1. 概要 - PowerShell 是cmdlet(command-let)的指令集合,类似unix的bash. - IDE: Windows PowerShell ISE,不区分大小写,可以用命 ...
- Powershell对象选择,排序和变量存储
PowerShell基础教程(17)——对象的选择.排序和变量存储 可以使用 Select-Object cmdlet 来创建新的.自定义的 Windows PowerShell 对象,后者包含的属性 ...
- 【189】◀▶ PowerShell 系统学习
参考网站如下: PowerShell 中文博客 PowerShell 博客——叹为观止 Mater-PowerShell 通过 PowerShell 编写脚本 Power ...
- 【黑客基础】Windows PowerShell 脚本学习(上)
视频地址:[黑客基础]Windows PowerShell 脚本学习 2019.12.05 学习笔记 1.$PSVersionTable :查看PowerShell的版本信息. 2.PowerShel ...
随机推荐
- 关于SYN洪泛攻击简介
在TCP三次握手中,server为了响应一个收到的SYN.分配并初始化连续变量和缓存.然后server发送一个SYNACK进行响应,并等待来自客户的ACK报文段. 假设某客户不发送ACK来完毕该三次握 ...
- 如何在其他类中实现继承自CFormView类的对象
今天项目开发中,我们创建了一个对话框资源,并创建了一个派生自CFormView的类(假设为CMyClassDlg)来管理它. CMyClassDlg.h #pragma once // CMyClas ...
- ASP.NET listBbox控件用法
ListBox基本功能使用方法 2011-06-09 13:23:16| 分类: .NET/C# | 标签:listbox基本功能使用方法 |举报 |字号大中小 订阅 ListBox基 ...
- 你应该知道CSS选择器技巧
什么是:before和:after? 该如何使用他们? :before是css中的一种伪元素,可用于在某个元素之前插入某些内容. :after是css中的一种伪元素,可用于在某个元素之后插入某些内容. ...
- java中Log4J的使用笔记
使用Log4J,程序员可以控制日志信息输送的目的地,包括控制台,文件,GUI组件和NT事件记录器,也可以控制每一条日志的输出格式,或通过定义每一条日志信息的级别,更加细致地控制日志的生成过程. Log ...
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- JSP page include taglib
page include taglib 语法:<%@ 指令名称 属性=值 属性=值 -%> ------------------- page 1.language 默认值java 2.ex ...
- bzoj 1088: [SCOI2005]扫雷Mine
题目链接 1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2525 Solved: 1495[Submi ...
- codeforces 603C. Lieges of Legendre sg函数
题目链接 n堆石子, 可以拿走一堆中的一颗, 或者将一堆数量为2*x的石子分为k堆x个的石子.k由题目给出. k分奇偶讨论. k为偶数时,k堆x个的石子异或结果为0: k为奇数时, k堆x个石子异或结 ...
- hdu 3980 Paint Chain sg函数
题目链接 给一个长度为n的环, 两个人轮流涂色, 每次涂m个连续的, 无法继续涂了就输. #include<bits/stdc++.h> using namespace std; #def ...