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 ...
随机推荐
- 什么是xss盲打
什么是xss盲打? 盲打仅仅是一种惯称的说法,就是不知道后台不知道有没有xss存在的情况下,不顾一切的输入xss代码在留言啊,feedback啊之类的地方,尽可能多的尝试xss的语句与语句的存在方式, ...
- 推荐两个不错的CAD二次开发(.Net)手册
推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...
- nginx args
$args $content_length $content_type $document_root $document_uri $host $http_user_agent $http_cookie ...
- Html页面加回滚
<div class="top-box"> <img src=" class="youlink-img" /><br / ...
- Android ActionBar详解(二)--->使用ActionBar显示选项菜单
MainActivity如下: package cc.testsimpleactionbar1; import android.os.Bundle; import android.app.Activi ...
- Developing your first FNC custom control
Friday, May 13, 2016 Some weeks ago, we released the TMS FNC UI Pack, a set of Framework Neutral Com ...
- Thml 小插件8 天气插件定制
网址:http://www.tianqi.com/plugin/
- 【基础】常用的机器学习&数据挖掘知识点
Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...
- DOM ISO - get current element's XPATH
DOM ISO - get current element's XPATH DOM ISO - get current element's XPATH
- activity的生命周期【转】
关于activity的生命周期,越来越感觉很重要.activity的生命周期有点像asp.net中page的生命周期,经历好几个过程.重写不同的阶段,可以完成不同的功能和效果.先上一张经典的生命周期图 ...