PowerShell Remove all user defined variable in PowerShell
2024-10-12 09:10:01
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
最新文章
- PHP钩子机制
- IOS时间传递机制简记
- Linux内核中的GPIO系统之(3):pin controller driver代码分析
- Code Jam 2008 APAC local onsites Problem C. Millionaire —— 概率DP
- 使用Lucene.net提升网站搜索速度整合记录
- awk使用入门
- Effective Java2读书笔记-类和接口(二)
- HDU 3401 Trade(单调队列优化)
- 团队作业4——第一次项目冲刺(Alpha版本)2st day
- Java课程设计-计算器 郑子杰(201521123021)
- Colossus: Successor to the Google File System (GFS)
- 2. ELK 之kibana 简介、获取、安装
- “玲珑杯”ACM比赛 Round #18---图论你先敲完模板(DP+思维)
- 2018-2019-2 网络对抗技术 20165227 Exp2 后门原理与实践
- MongoDB索引的种类与使用
- eclipse 中使用中文JAVA api文档
- Linux下adb的配置
- WebService-php- 2(17)
- Codeforces Round #416 (Div. 2) A+B
- UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)