powershell - 删除所有变量 [英] powershell - Remove all variables

查看:71
本文介绍了powershell - 删除所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在脚本开始时删除所有用户创建的变量.

I want to remove all user-created variables at the start of a script.

目前我正在执行 Remove-Variable -Name * 但它也尝试删除所有系统变量,从而导致大量错误消息.

Currently I am doing Remove-Variable -Name * but it tries to delete all system variables as well, resulting in lot of error messages.

还有其他方法吗?

推荐答案

由于所有系统变量都是只读或常量,因此您可以将错误静音:

Since all the system variables are read-only or constant anyway you can just silence the errors:

Remove-Variable * -ErrorAction SilentlyContinue

但无可否认,您可能无论如何都应该排除一些:

But admittedly, you should probably exclude a few anyway:

Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0

这篇关于powershell - 删除所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆