PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Geek Sync Live

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Scripts

03-21-2015 - Run CMD\BAT as PS1
03-12-2015 - Create organization tree
03-04-2015 - Rename Double Driver oem files

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Finding Read-Only and Constant Variables

All PowerShell versions

Some variables are protected and cannot be changed. To identify these, take a look at this line:

Get-Variable |   
  Where-Object { $_.Options -like '*Constant*' -or $_.Options -like '*ReadOnly*' } |
  Select-Object -Property Name, Options, Description

The result looks similar to this:

Name                                           Options Description               
----                                           ------- -----------               
?                                   ReadOnly, AllScope Status des letzten Befehls
ConsoleFileName                     ReadOnly, AllScope Name der aktuellen Kons...
Error                                         Constant                           
ExecutionContext                    Constant, AllScope Die für Cmdlets verfügb...
false                               Constant, AllScope Boolean False             
HOME                                ReadOnly, AllScope Ordner mit dem Profil d...
Host                                Constant, AllScope Ein Verweis auf den Hos...
PID                                 Constant, AllScope Aktuelle Prozess-ID       
PSCulture                           ReadOnly, AllScope Die Kultur der aktuelle...
PSHOME                              Constant, AllScope Der übergeordnete Ordne...
psISE                                         Constant                           
PSUICulture                         ReadOnly, AllScope Die Benutzeroberflächen...
psUnsupportedConsoleAppl...                   Constant                           
PSVersionTable                      Constant, AllScope Versionsinformationen f...
ShellId                             Constant, AllScope "ShellID" gibt die aktu...
true                                Constant, AllScope Boolean True            

The interesting part is how Where-Object identified the variables. The code used a string comparison and –like. That’s because the variable options are flags, and flags can be combined. By using –like and placeholders, you can (halfway) safely identify the flag you are after, even if additional flags have been set.

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.