Working with Private Variables
Working with Private Variables
By default, PowerShell inherits by default variables downstream so subsequent scopes can “see” the parent variables. If you want to turn off variable inheritance altogether, you should use the prefix “private:.”. This way, variables will only work in the scope in which they are defined in, and they will neither inherit upstream or downstream:
$ private:a = 1
Function test {
“variable a contains $ a”
$ a = 2
“variable a contains $ a”
}
test
variable a contains
variable a contains 2
$ a
1
You should note that defining a variable as private will not overwrite an existing variable. So, if you had previously defined a variable “a” without the private: prefix, you would not be able to turn it into a private variable. To make sure, you should either start a new fresh PowerShell environment, or delete the variable before creating your private variable:
Remove-Variable a -ErrorAction SilentlyContinue
Changing Console Colors
Like any other console window, PowerShell has 16 pre-defined colors that you can choose from to set background and foreground color. You can use two different approaches to set the values. Both set the console background color to “Blue”:
$ host.UI.RawUI.BackgroundColor = ‘Blue’
[System.Console]::BackgroundColor = ‘Blue’
You can enter Clear-Host to have the entire console background repainted.
Read this full tip here:
http://powershell.com/cs/blogs/tips/archive/2010/10/18/working-with-private-variables.aspx
View all of last weeks PowerShell.com tips here:
http://powershell.com/cs/blogs/tips/archive/2010/10/18/working-with-private-variables.aspx
http://powershell.com/cs/blogs/tips/archive/2010/10/19/changing-console-colors.aspx
http://powershell.com/cs/blogs/tips/archive/2010/10/20/resetting-console-colors.aspx
http://powershell.com/cs/blogs/tips/archive/2010/10/21/clearing-console-content.aspx
http://powershell.com/cs/blogs/tips/archive/2010/10/22/listing-available-culture-ids.aspx
Follow @PowerTip on Twitter for daily Tips, Tricks, and Hints!
Article from articlesbase.com
More Powershell Scripting Articles
Incoming search terms:
- facebook private script
- powershell private variable in function
- powershell private variable scope
- powershell variable löschen
- powershell variable private löschen
- private variable in shell script
- shell define private variable
- shell private variables
- shell script private variables
Related posts:

Leave a Reply