Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Is it possible to create a Constant variable?

Reply
 
Old 08-12-2006   #1 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

Is it possible to create a Constant variable?

As the subject says, is it possible to create a constant variable?

I was able to set "ReadOnly" option for variables (that i actually wanted to
declare as constants) but setting the variable option to "Constant" complains
that I need to declare a variable as a constant during creation time of a
variable

[^_^]PS[167]>$a = "constant string"
[^_^]PS[168]>(gi a).options = "Constant"
Exception setting "Options": "Existing variable a cannot be made constant.
Variables can be made constant only at creat
ion time."
At line:1 char:8
+ (gi a).o <<<< ptions = "Constant"

Is there a special keyword to create a constant variable? or is creating a
constant available only through Cmdlets in the current version of PowerShell?

====================
Sung M Kim

Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-12-2006   #2 (permalink)
Adam Milazzo


 
 

Re: Is it possible to create a Constant variable?

dance2die wrote:
> As the subject says, is it possible to create a constant variable?

Yes, use the new-variable command.

% new-variable -Option Constant varName "value"
% $varName
value
% $varName = 5
Cannot overwrite variable varName because it is read-only or constant.
My System SpecsSystem Spec
Old 08-12-2006   #3 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

Re: Is it possible to create a Constant variable?

"Adam Milazzo" wrote:
> % new-variable -Option Constant varName "value"
> % $varName
> value
> % $varName = 5
> Cannot overwrite variable varName because it is read-only or constant.
>



i have overlooked "*-Variable" cmdlets...
Thank you for the tips there, Adam

====================
Sung M Kim

Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-12-2006   #4 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Is it possible to create a Constant variable?

But that does involve using a cmdlet.
"dance2die" <dance2die@discussions.microsoft.com> wrote in message
news:79EBC508-8A7F-4502-AEE2-DF51252CEC25@microsoft.com...
> "Adam Milazzo" wrote:
>> % new-variable -Option Constant varName "value"
>> % $varName
>> value
>> % $varName = 5
>> Cannot overwrite variable varName because it is read-only or constant.
>>

>
>
> i have overlooked "*-Variable" cmdlets...
> Thank you for the tips there, Adam
>
> ====================
> Sung M Kim
>
> Please don''t bother me with spam...
>



My System SpecsSystem Spec
Old 08-12-2006   #5 (permalink)
Adam Milazzo


 
 

Re: Is it possible to create a Constant variable?

Alex K. Angelopoulos [MVP] wrote:
> But that does involve using a cmdlet.

I think he meant "Is the ability to create constant variables limited to
Cmdlets?"

And I guess it is, but thankfully there's a Cmdlet that exposes the
ability to the shell. :-)
My System SpecsSystem Spec
Old 08-12-2006   #6 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

Re: Is it possible to create a Constant variable?

"Alex K. Angelopoulos [MVP]" wrote:
> But that does involve using a cmdlet.


"dance2die" wrote:
> Cmdlets in the current version of PowerShell?


Ah, by the above statement, hmm i meant creating a custom cmdlet in .NET
I should have been more elaborate on describing what I really wanted to
accomplish. Sorry about the confusion there~

====================
Sung M Kim

Please don''t bother me with spam...



> "dance2die" <dance2die@discussions.microsoft.com> wrote in message
> news:79EBC508-8A7F-4502-AEE2-DF51252CEC25@microsoft.com...
> > "Adam Milazzo" wrote:
> >> % new-variable -Option Constant varName "value"
> >> % $varName
> >> value
> >> % $varName = 5
> >> Cannot overwrite variable varName because it is read-only or constant.
> >>

> >
> >
> > i have overlooked "*-Variable" cmdlets...
> > Thank you for the tips there, Adam
> >
> > ====================
> > Sung M Kim
> >
> > Please don''t bother me with spam...
> >

>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
dynamically create variable? PowerShell
Vista Explorer Create New Item Not Visible on Create - Bug or Feature??? Vista General
how to assign values to array and how to create array via variable PowerShell
How can I ensure that a variable is a built-in powershell variable? PowerShell
How To: Create Dynamic Variable Names in Scripts PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46